日期:2014-05-16  浏览次数:20390 次

Javascript新手求教
Javascript中的构造函数和其他的函数本质上的区别是什么呢?我随便new一个函数会有什么结果呢?


------解决方案--------------------
没区别,一个函数new一下会出现一个对象
------解决方案--------------------
All functions in Javascript can be new, but then it is not meaning a function, it is a class so that I list an example to you:

function SuperTab()
{
this.testName = "aa";
alert(this == window);
alert(window.testName);
}

if you directly call the SuperTab, will alert true and "aa", because the scope is window for now.

if you new the SuperTab, will alert false and "undefined", because the scope is SuperTab, the variable "testName" is avaiable in the SuperTab.

------解决方案--------------------
I specially suggest you some articles for learning the Javascript: Javascript Articles
------解决方案--------------------
详细的你可以去看看javascript权威指南里面关于原型的章节,讲的非常详细。
你就把构造函数当成一般面向对象语言里面的构造函数好了, 用 new调用函数,来产生类的实例
------解决方案--------------------
探讨

引用:

I specially suggest you some articles for learning the Javascript: Javascript Articles

非常感谢啊,你上面那段关于函数的英文解释让我受益良多,从哪能找到更多的深入解释呢?貌似是英文版的指南吧?原文在哪?

------解决方案--------------------
先产生一个 Object构造函数的对象,也就是 var o = new Object 或 var o = {}这样的
然后在 这个最初的对象上面调用构造函数, 类似与 构造函数.call(o) 这样
且新对象能共享构造函数的原型对象中的属性
------解决方案--------------------
应该没有区别吧
------解决方案--------------------
http://topic.csdn.net/u/20090217/13/653f7949-9e0c-4e99-adb3-dcbc99fca8d5.html?6940

今天早上看到的,感觉对你能有帮助