相关疑难解决方法(0)

javascript中原型的含义

我写的继承的短代码reader来自Person:

<script>

/* Class Person. */
function Person(name) {
    this.name = name;
}

Person.prototype.getName = function() {
    return this.name;
}

var reader = new Person('John Smith');
alert(reader.getName());

</script>
Run Code Online (Sandbox Code Playgroud)

或者,我可以删除行 Person.prototype.getName = function() { return this.name; }并在Person对象中创建它.例如

<script>
/* Class Person. */
function Person(name) {
    this.name = name;
    this.getName = function() { return this.name;}
}

var reader = new Person('John Smith');
alert(reader.getName());

</script>
Run Code Online (Sandbox Code Playgroud)

getName()在这两种情况下调用时,我得到了相同的结果.那他们有什么不同?

javascript

17
推荐指数
1
解决办法
3930
查看次数

为什么 Array.prototype 也是一个数组?

我认为每个原型都应该是一个对象。

为什么?

Array.isArray( Array.prototype ) // true

developer.mozilla.org没有任何解释

javascript prototype

3
推荐指数
1
解决办法
949
查看次数

标签 统计

javascript ×2

prototype ×1