-1
<script>
var myObject = function(name){
    this.name = name;
    return this;
};

myObject.prototype.getName = function(){
    return this.name;
};
console.log(myObject instanceof Function); // true
</script>

问题:

如何理解这一行:console.log(myObject instanceof Function); // true?如果我们要创建实例,我们需要使用new关键字,对吗?比如:var myObject = new Function();那么 myObject 怎么可能是 Function 的实例呢?

4

2 回答 2

1

myObject是一个函数,每个函数都是Function.

console.log(myObject instanceof Function); // true
console.log(new myObject('foo') instanceof myObject); // true
于 2013-06-18T09:03:29.067 回答
0

如果我们要创建实例,我们需要使用 new 关键字,对吗?

通常。不过,函数表达式也返回函数对象。

于 2013-06-18T09:03:16.823 回答