为什么要在函数定义之外引用静态变量(函数属性)
Why refer to static variables (function properties) outside the function definition?
我正在查看JavaScript中的静态变量,我注意到我以前看到过的东西,函数被定义,在函数定义之后,函数原型被更新:
function MyClass () { // constructor function
//function definition here
}
//now add a (static?) method *outside* the function definition
MyClass.prototype.publicMethod = function () {
alert(this.publicVariable);
};
//add a static property *outside* the function definition
MyClass.staticProperty = "baz";
这是我的问题 - 为什么不在函数定义中定义它们,如下所示:
function MyFunc(){
MyFunc.staticVar = 1;
//static method showing static var
MyFunc.showVarStatic = function(){
alert(MyFunc.staticVar);
}
//instance method referring to static var
this.showVarInstance = function(){
alert(MyFunc.staticVar);
}
//instance method - doesn't change static var
this.inc1 = function(){
this.staticVar += 1;//no such property
}
//static method, changes var
this.inc2 = function(){
MyFunc.staticVar += 1;//increments static property
}
}
这似乎在IE8,FF和Chrome中表现得像预期的那样。 这只是个人喜好/风格的事情吗? 我喜欢它,因为我的整个功能都包含在那些大括号中。
[编辑:在做了更多的阅读和实验之后,我对javascript函数是如何是构造函数的,以及它们与C#类的区别 - 这是我用来演示这一点的一些代码]
//this is deceiving, notSoStaticVar won't exist until MyFunc1 has been run
//and then it will be reset whenever MyFunc1 (a constructor) is run
function MyFunc1(){
MyFunc1.notSoStaticVar = "I belong to MyFunc1";
this.instanceVar = "I belong to instances of MyFunc1";
}
//this code will be run inline one time,
//so the static property of MyFunc2 will exist
//(I like how all the functionality is in one code block, but it's kind of messy)
MyFunc2 = (function(){
var temp = function(){
this.instanceVar = "I belong to an instance of MyFunc2";
}
temp.staticVar = "I belong to MyFunc2";
return temp;
})();
//this seems to be equivalent to MyFunc2, but the code is cleaner
MyFunc3 = function(){
}
MyFunc3.prototype.instanceVar = "I belong to an instance of MyFunc3";
MyFunc3.staticVar = "I belong to MyFunc3";
//tests
console.log(MyFunc1.notSoStaticVar);//undefined!
var a = new MyFunc1();
console.log(MyFunc1.notSoStaticVar);//"I belong to MyFunc1"
console.log(a.instanceVar);//"I belong to instances of MyFunc1"
MyFunc1.notSoStaticVar = "I will be changed when another instance of MyFunc1 is created";
console.log(MyFunc1.notSoStaticVar);//"I will be changed when another instance of MyFunc1 is created"
var b = new MyFunc1();
console.log(MyFunc1.notSoStaticVar);//"I belong to MyFunc1" - was reset by constructor!
//now test MyFunc2
console.log(MyFunc2.staticVar);//"I belong to MyFunc2"
MyFunc2.staticVar = "I am not affected by the construction of new MyFunc2 objects";
var c = new MyFunc2();
console.log(c.instanceVar);//"I belong to an instance of MyFunc2"
console.log(MyFunc2.staticVar);//"I am not affected by the construction of new MyFunc2 objects"
//now test MyFunc3
console.log(MyFunc3.staticVar);//"I belong to MyFunc3"
MyFunc3.staticVar = "I am not affected by the construction of new MyFunc3 objects";
var d = new MyFunc3();
console.log(d.instanceVar);//"I belong to an instance of MyFunc3"
console.log(MyFunc3.staticVar);//"I am not affected by the construction of new MyFunc3 objects"
//interesting
console.log(c);//"temp" <-- not really intuitive!
console.log(d);//"MyFunc3" <-- makes sense
简而言之:性能。
在函数中定义它们将导致每次调用构造函数时重新定义它们。
虽然这将按预期运行,但它只是无缘无故的开销。
因为这
将为每个对象实例添加唯一的函数。这会消耗通常不需要的额外内存开销。
它在某些情况下可能很有用,例如当函数应该引用局部变量时,但如果不是这种情况,它们应该在原型上。
此外,静态的将不断被覆盖。
相关文章:
- 访问嵌套函数结构中的JavaScript父函数变量
- Javascript函数变量传递语法问题
- JavaScript 函数变量范围问题
- 在 promise 中获取构造函数变量
- 将函数结果传递给Javascript中的另一个函数变量
- 用对象设置函数变量's值
- Javascript函数变量应生成错误,而不是未定义的错误
- 我可以访问用户脚本中的匿名函数变量吗?
- 在实例化期间使用构造函数变量
- 为什么在 JavaScript 中不能用函数变量覆盖字符串变量
- JavaScript 允许调用函数访问调用函数变量
- 如何在嵌套原型中访问 JavaScript 构造函数变量
- 对函数变量的 JavaScript 控制台日志输出感到困惑
- 如何将函数变量传递给主干模型上的 jQuery 事件
- 如何访问构造函数变量
- 实例函数变量不会更改
- 为什么函数变量在变量之后是未定义的
- 用内部函数变量更改外部函数变量?Javascript
- Javascript - 使用闭包递增静态函数变量模拟
- 异步 JavaScript 静态函数变量