[英]Object.defineProperty - scope and use inside function returning object?
我已经学习了几天的JavaScript(使用最新的基于64位Debian Jessie的Google Chrome构建),现在我一直在了解Object.defineProperty
工作原理。 我有以下页面:
function createPerson() {
var person = {
firstName: 'Lilly',
lastName: 'Louis',
};
Object.defineProperty(person, 'fullName', {
get: function() {
return this.firstName + ' ' + this.lastName;
},
set: function(name) {
var words = name.split(' ');
this.firstName = words[0] || '';
this.lastName = words[1] || '';
}
});
}
var p1 = createPerson();
console.log("Person's default name is \"" + p1.fullName + "\"");
我收到Uncaught TypeError: Cannot read property 'fullName' of undefined
在console.log(...)
内部使用该属性的行Uncaught TypeError: Cannot read property 'fullName' of undefined
。 如果我将person
的定义与Object.defineProperty
放在函数之外,则一切正常。 如果像这样创建p1
之后仅将 Object.defineProperty
移到函数外:
var p1 = createPerson();
Object.defineProperty(p1, 'fullName', {
get: function() {
return this.firstName + ' ' + this.lastName;
},
set: function(name) {
var names = name.split(' ');
this.firstName = names[0] || '';
this.lastName = names[1] || '';
}
});
我收到了Uncaught TypeError: Object.defineProperty called on non-object
然后控制台指出它是在(anonymous function)
(即p1
)上调用的,这更加令人困惑,因为我刚刚读过,如果我分配了callPerson
不带括号将其传递给我的p1
,将函数对象传递给它,否则我调用函数本身并将其返回值(如果没有返回,则返回undefined )给p1
。
我觉得这两个问题是完全分开的,但是由于我不确定100%是否已决定将两个问题都放在同一个问题中。 有什么建议我在这里做错了吗? 我可以通过简单的方法来定义我的getter
和setter
方法
function createPerson() {
var person = {
firstName: 'Lilly',
lastName: 'Louis',
getFullName: function() {
return ...;
}
setFullName: function(name) {
...
}
};
}
但我想了解这里的情况。 :D
您的defineProperty
调用绝对正确。 问题在于createPerson
从不返回任何内容,因此调用它的结果是undefined
。 你要return person;
在末尾:
function createPerson() { var person = { firstName: 'Lilly', lastName: 'Louis', }; Object.defineProperty(person, 'fullName', { get: function() { return this.firstName + ' ' + this.lastName; }, set: function(name) { var words = name.split(' '); this.firstName = words[0] || ''; this.lastName = words[1] || ''; } }); return person; // <======= } var p1 = createPerson(); console.log("Person's default name is \\"" + p1.fullName + "\\"");
或者,使其成为构造函数(与new
)而不是工厂函数,然后在函数内使用this
。 在这种情况下,我们将从名称中删除create
,并且(通常)不使用return this;
在端部,因为如果该函数不返回不同的对象,通过默认的结果new XYZ
是由创建该对象new
操作者,并提供给该功能的this
:
function Person() { this.firstName = 'Lilly'; this.lastName = 'Louis'; Object.defineProperty(this, 'fullName', { get: function() { return this.firstName + ' ' + this.lastName; }, set: function(name) { var words = name.split(' '); this.firstName = words[0] || ''; this.lastName = words[1] || ''; } }); } var p1 = new Person(); console.log("Person's default name is \\"" + p1.fullName + "\\"");
这是因为您创建了一个person
变量,但从未返回它。 因此,该函数未返回任何内容,并且赋值是不确定的:
function createPerson() {
var person = {
firstName: 'Lilly',
lastName: 'Louis',
};
Object.defineProperty(person, 'fullName', {
get: function() { return this.firstName + ' ' + this.lastName; },
set: function(name) {
var words = name.split(' ');
this.firstName = words[0] || '';
this.lastName = words[1] || '';
}
});
return person; // Add this line
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.