带有 setter 方法的 JavaScript 属性不是真正的属性
JavaScript properties with setter methods aren't real properties?
function Name(first, last) {
this.first = first;
this.last = last;
this.fullName = first + " " + last
}
Name.prototype = {
get fullName() {
return this.first + " " + this.last;
},
set fullName(name) {
var names = name.split(" ");
this.first = names[0];
this.last = names[1];
}
};
var person = new Name("Foo", "Bar");
// person.fullName = "Foo Bar"
person.hasOwnProperty("fullName") // false
有没有办法返回属性?
hasOwnProperty 不会检查对象的原型链,在您的情况下,人get
和set
都是通过原型链继承
拥有自己的财产
从 Object 派生的每个对象都继承 hasOwnProperty 方法。 此方法可用于确定对象是否具有 指定属性作为该对象的直接属性;不像在 运算符,此方法不检查对象的原型 链。
function Name(first, last) {
this.first = first;
this.last = last;
this.fullName = first + " " + last;
}
var person = new Name("Foo", "Bar");
console.log(person.fullName);
console.log(person.hasOwnProperty("fullName")); // true
console.log(Name.prototype.hasOwnProperty("fullName")); // false
for (var prop in person) {
console.log(prop);
}
console.log('');
Name.prototype = {
get fullName() {
return this.first + " " + this.last;
},
set fullName(name) {
var names = name.split(" ");
this.first = names[0];
this.last = names[1];
}
};
var person2 = new Name("Foo", "Bar");
console.log(person2.fullName);
console.log(person2.hasOwnProperty("fullName")); // false
console.log(Name.prototype.hasOwnProperty("fullName")); // true
报价@JLRishe
当您使用 this.fullName = "...";在你的构造函数中,你是 调用继承的 setter,而不是添加新属性。
如果你想找到这样的属性,你可以使用一个 for...在声明中:
for (var prop in person) {
console.log(prop);
}
InvernoMuto指出的,Object.hasOwnProperty("fullName")
返回false
,因为它不是person
自己的属性;它是通过原型链继承的。在构造函数中使用 this.fullName = "...";
时,您是在调用继承的 setter,而不是添加新属性。
如果要查找此类属性,可以:
- 使用
for..in
循环:
for (var prop in person) {
// this will iterate over EVERY property in person's prototype chain
}
- 在构造函数中附加属性:
function Name(first, last) {
this.first = first;
this.last = last;
Object.defineProperty(this, "fullName", {
get: function() {
return this.first + " " + this.last;
},
set: function(name) {
var names = name.split(" ");
this.first = names[0];
this.last = names[1];
}
});
}
var person = new Name("Ronald", "McDonald");
console.log(person.hasOwnProperty("fullName")); // true
- 在构造函数中创建一个全新的对象,使用那里的 get/set 语法。在这种情况下,我们不会使用
new
关键字,而只需调用函数:
function Name(first, last) {
return {
first: first,
last: last,
get fullName() { return this.first + " " + this.last; },
set fullName(name) {
var names = name.split(" ");
this.first = names[0];
this.last = names[1];
}
};
};
var person = Name("Ronald", "McDonald");
console.log(person.hasOwnProperty("fullName")); // true
根据MDN,"get语法将对象属性绑定到查找该属性时将调用的函数。这意味着,每当您尝试访问Name
实例的fullName
时(例如 person.fullName
),javascript 将委托给该 getter 函数,该函数恰好位于 Name.prototype
对象上。
此外,"set 语法将对象属性绑定到尝试设置该属性时要调用的函数。因此,即使显式地为每个实例提供一个fullName
属性,javascript 实际上也会在原型上查看该set
函数。如果你想解决这个问题,并且person.hasOwnProperty("fullName")
返回 true,你可以做这样的事情:
function Name(first, last) {
this.first = first;
this.last = last;
this.fullName = first + " " + last
}
Name.prototype.setFullName = function(fullName){
var names = fullName.split(" ");
this.first = names[0];
this.last = names[1];
this.fullName = fullName;
};
var person = new Name("Foo", "Bar");
// person.fullName = "Foo Bar"
person.hasOwnProperty("fullName") // true
- 保存具有相同属性 JavaScript 的元素上的值
- DOM 元素上的空样式属性:JavaScript
- 基于一个属性Javascript检索多个对象
- xml获取属性javascript不会;无法处理childNodes
- 当输入填充了必需的属性javascript/html/css时,如何更改焦点颜色
- 如何处理未定义属性的未定义属性 javascript 类型错误
- 向嵌套对象添加属性 - Javascript
- 属性 Javascript 的访问属性
- 更改链中的 css 属性 - JavaScript
- 更改属性 Javascript 方式的值
- 如何从 url 解析 xml 获取属性 javascript
- 语法错误:缺少:在属性 javascript 之后
- 创建 if 条件基于 CSS 属性 -- JavaScript
- 访问节点的属性javascript二进制搜索树
- 访问父函数属性Javascript
- 为什么未定义此对象的属性?Javascript
- 访问私有属性Javascript OOP
- 访问数组中的属性?(Javascript)
- 动态添加参数作为对象的属性-JavaScript
- 冲突/方法/属性Javascript错误