在构造函数中访问包含对象
Access the containing object in a constructor function
我想在调用包含在该对象中的函数作为构造函数时访问父对象。看这个例子:
var users = {
// The count of users
count: 0,
// Naturally _this is undefined
_this: this,
// Constructor function
CreateUser: function (name, email) {
this.name = name;
this.email = email;
// Is this the only way to access the containing object?
users.count++;
},
getCount: function () {
return this.count;
},
};
如果我尝试调用 CreateUser
函数作为构造函数,那么 this
将是对 new
运算符创建的空白对象的引用。看到这个:
var user = new users.CreateUser('Rick', 'rick@example.com')
在这种情况下,如何在不显式引用的情况下访问包含 users
的对象?
将其作为显式参数传递。
var users = {
count: 0,
_this: this,
// Constructor function
CreateUser: function (name, email, parent) {
this.name = name;
this.email = email;
// Is this the only way to access the containing object?
parent.count++;
},
getCount: function () {
return this.count;
},
};
var user = new users.CreateUser('Rick', 'rick@example.com', users);
看起来您正在寻找的是 static 属性,它仍将在 JS 中出现。
不过这是一项实验性功能。
目前:
Rectangle.staticWidth = 20;
Rectangle.prototype.prototypeWidth = 25;
因为你在这里所做的在某种程度上类似于 Joshua Bloch's idea of a static factory method or factory method pattern 一般,将 new
关键字移动到 CreateUser 方法是有意义的 用户对象。
通过这样做,您可以使用闭包来保存对 users 对象的引用,实现嵌套构造函数并使用 new
关键字调用它。
工作示例:
var users = {
count: 0,
CreateUser: function (name, email) {
var self = this;
const createUser = function(name, email) {
this.name = name;
this.email = email;
self.count++;
}
return new createUser(name, email)
},
getCount: function () {
return this.count;
},
};
var user = users.CreateUser('Rick', 'rick@example.com')
console.log(user)
console.log(users.getCount())
users.CreateUser('Morty', 'morty@example.com')
users.CreateUser('Jerry', 'apples@example.com')
console.log(users.getCount())
我想在调用包含在该对象中的函数作为构造函数时访问父对象。看这个例子:
var users = {
// The count of users
count: 0,
// Naturally _this is undefined
_this: this,
// Constructor function
CreateUser: function (name, email) {
this.name = name;
this.email = email;
// Is this the only way to access the containing object?
users.count++;
},
getCount: function () {
return this.count;
},
};
如果我尝试调用 CreateUser
函数作为构造函数,那么 this
将是对 new
运算符创建的空白对象的引用。看到这个:
var user = new users.CreateUser('Rick', 'rick@example.com')
在这种情况下,如何在不显式引用的情况下访问包含 users
的对象?
将其作为显式参数传递。
var users = {
count: 0,
_this: this,
// Constructor function
CreateUser: function (name, email, parent) {
this.name = name;
this.email = email;
// Is this the only way to access the containing object?
parent.count++;
},
getCount: function () {
return this.count;
},
};
var user = new users.CreateUser('Rick', 'rick@example.com', users);
看起来您正在寻找的是 static 属性,它仍将在 JS 中出现。
不过这是一项实验性功能。
目前:
Rectangle.staticWidth = 20;
Rectangle.prototype.prototypeWidth = 25;
因为你在这里所做的在某种程度上类似于 Joshua Bloch's idea of a static factory method or factory method pattern 一般,将 new
关键字移动到 CreateUser 方法是有意义的 用户对象。
通过这样做,您可以使用闭包来保存对 users 对象的引用,实现嵌套构造函数并使用 new
关键字调用它。
工作示例:
var users = {
count: 0,
CreateUser: function (name, email) {
var self = this;
const createUser = function(name, email) {
this.name = name;
this.email = email;
self.count++;
}
return new createUser(name, email)
},
getCount: function () {
return this.count;
},
};
var user = users.CreateUser('Rick', 'rick@example.com')
console.log(user)
console.log(users.getCount())
users.CreateUser('Morty', 'morty@example.com')
users.CreateUser('Jerry', 'apples@example.com')
console.log(users.getCount())