在阅读了 JavaScript 中的原型(prototype)之后,我创建了这个 JSFiddle测试一些关于函数如何工作的事情。
我了解到这两个函数的原型(prototype)是不一样的。但为什么 '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
是存在于 inside test1
和 test2< 函数体中的函数
当它们执行时。
它们不是 test1
和 test2
的属性,所以 test1.foo
和 test2.bar
是两者都是 undefined
。
关于JavaScript 函数和原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23149631/