0
如果可以创建一个Element.prototype
作为字符串?作为字符串的元素原型
比如:
代替:
Element.prototype.height = function() { return this.offsetHeight; };
写的是这样的:
Element.prototype.height = Element.offsetHeight;
如果可以创建一个Element.prototype
作为字符串?作为字符串的元素原型
比如:
代替:
Element.prototype.height = function() { return this.offsetHeight; };
写的是这样的:
Element.prototype.height = Element.offsetHeight;
你可以尝试这样的:
Element.prototype = {
get height():{ return this.offsetHeight; }
constructor : Element
};
用法:
new Element().height //without parenthesis
更多,请参阅Object getters/setters。
你尝试过吗? – Joseph
是的,结果是:undefined – John
是否定义了'Element.offsetHeight'?请注意,这两个片段并不等同。第一个从* instance *获得'offsetHeight',这是构造函数的第二个。所以,如果'offsetHeight'是实例的一个属性,并且在初始化过程中确定了,则必须使用函数通过'height'来代理该值。 –