阅读有关JavaScript的原型后,我创建了JSFiddle来测试有关函数工作方式的一些信息。
我了解这两个函数的原型不相同。但是,为什么“ foo”和“ bar”函数具有相同的名称,却又做不同的事情,为什么呢?
代码:
var test1 = function(){
function foo(){
alert('test1 foo function');
}
}
var test2 = function(){
function bar(){
alert('test2 foo function');
}
}
if (test1.foo === test2.bar) {
alert("they're the same")
}
if (test1.prototype === test2.prototype){
alert("they're not the same")
}
最佳答案
foo
和bar
是在执行时test1
和test2
的功能体内的功能。
它们不是test1
和test2
的属性,因此test1.foo
和test2.bar
都是undefined
。
关于javascript - JavaScript函数和原型(prototype),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23149631/