【发布时间】:2020-07-02 15:09:07
【问题描述】:
我是 js 新手,我试图了解是否可以访问使用对象构造函数创建的所有对象的参数。例如: 我有一个对象构造函数:
function objectconstructor (width){
this.color=width;
this.element = document.createElement('div')
element.style.backgoundColor = this.width + 'px';
}
// Now I have two objects created with the objectconstructor:
var object1 = new objectconstructor (100)
var object2 = new objectconstructor (200)
// Now I have the following function
function () {
if((object1.width>100) || (object2.width>100)){
alert('hi')
}
}
我想缩短 if 语句,例如:
if (All_objects_created_with_object_constructor_function.width > 100) {
alert('hi')
}
【问题讨论】:
-
不,如果没有您自己的维护初始化值列表的代码,您将无法做到这一点。
-
由于属性的值可以稍后更改,您能否详细说明您要检查当前值还是初始值(
width参数)?此外,您拥有的代码和您想要的代码并不相同。当前代码中的条件在至少一个对象的width大于100 时通过,目标是所有对象的宽度都应大于100。哪一个是正确的?
标签: javascript parameter-passing