Javascript 什么是;获得;原型中的对象是什么?
我知道Javascript 什么是;获得;原型中的对象是什么?,javascript,prototype,Javascript,Prototype,我知道ElementMetrics是构造函数,原型对象有键和值。但原型对象中的get意味着什么呢 function ElementMetrics(element) { this.element = element; this.width = this.boundingRect.width; this.height = this.boundingRect.height; this.size = Math.max(this.width, this.height); } Elemen
ElementMetrics
是构造函数,原型对象有键和值。但原型对象中的get
意味着什么呢
function ElementMetrics(element) {
this.element = element;
this.width = this.boundingRect.width;
this.height = this.boundingRect.height;
this.size = Math.max(this.width, this.height);
}
ElementMetrics.prototype = {
get boundingRect () {
return this.element.getBoundingClientRect();
},
furthestCornerDistanceFrom: function(x, y) {
var topLeft = Utility.distance(x, y, 0, 0);
var topRight = Utility.distance(x, y, this.width, 0);
var bottomLeft = Utility.distance(x, y, 0, this.height);
var bottomRight = Utility.distance(x, y, this.width, this.height);
return Math.max(topLeft, topRight, bottomLeft, bottomRight);
}
};
这意味着它可以像对象上的属性一样被调用,但实际上它是一个函数
function Person () {
this.firstName = "John";
this.lastName = "Smith";
}
Person.prototype = {
setFirstName: function (name) {
this.firstName = name;
},
setLastName: function (name) {
this.lastName = name;
},
get fullName () {
// we can perform logic here
return this.firstName + " " + this.lastName;
}
};
var person = new Person();
person.setFirstName("Nicole");
person.setLastName("Wu");
console.log("The persons name is " + person.fullName); // The persons name is Nicole Wu
它是一个getter(访问器属性):哦,我明白了!这种写入方式可以设置键的动态值。“我说得对吗?”尼科尔乌是的,在某种程度上。这意味着您可以在获取属性时执行逻辑。如果我有帮助,请接受我的回答:)