dag*_*und 3 javascript object new-operator
使用或不使用new关键字调用javascript函数之间有区别吗?例如,如果我有这个功能:
function computer(){
this.hardDrive = "big";
this.processor = "fast";
}
Run Code Online (Sandbox Code Playgroud)
然后我以两种不同的方式称呼它:
var hp = computer();
var hp = new computer();
Run Code Online (Sandbox Code Playgroud)
什么是两个函数调用之间的区别?
没有new,this指的是全局对象,而不是从函数返回的任何对象.
如果你要执行你的代码,你会发现第一个hp是undefined,而第二个是[object Object].此外,由于显而易见的原因,第一个没有属性,hardDrive或者processor第二个属性.
在第一个示例中,您的两个属性已添加到window对象中.
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |