Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想让一个全局变量不仅被一个对象使用,我该怎么做?
//Want to take the type of str and myVar str.type myVar.type
只是不要使用关键字var,它将是全局的,但这不会使其成为对象的属性。我只是将 Object 属性分配给 Object 外部的变量,然后它可以被下面的所有 Objects 继承,例如:
var
function Obj(val){ this.prop = val; } var obj = new Obj('nice'), whatever = obj.prop; // now whatever is accessible to other Objects below