Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中重置原型。为什么它会破坏原型继承链?_Javascript_Prototype - Fatal编程技术网

在Javascript中重置原型。为什么它会破坏原型继承链?

在Javascript中重置原型。为什么它会破坏原型继承链?,javascript,prototype,Javascript,Prototype,我有以下代码: function PrintStuff(docs) { this.docs = docs; } PrintStuff.prototype.print = function() { console.log(this.docs) } var printer = new PrintStuff("Hello World"); printer.print() console.log(Object.getPrototypeOf(printer)) console.log(Prin

我有以下代码:

function PrintStuff(docs) {
  this.docs = docs;
}

PrintStuff.prototype.print = function() {
  console.log(this.docs)
}

var printer = new PrintStuff("Hello World");
printer.print()
console.log(Object.getPrototypeOf(printer))
console.log(PrintStuff.prototype)
console.log(printer instanceof(PrintStuff))
//true

PrintStuff.prototype = {}
console.log(printer instanceof(PrintStuff))
//false
  • instanceof是什么样的方法?为什么不在对象上调用它
  • 为什么设置PrintStuff的原型会破坏printer对象的继承链
  • instanceof
    是运算符,而不是方法。你写的是
    1+(2)

  • printsuff.prototype
    不是
    printsuff
    的原型;它是由
    PrintStuff
    构造函数创建的对象的原型。替换它时,在此之后创建的任何对象将不再具有
    .print
    方法<代码>打印机仍然如此,因为它仍然拥有旧的原型

  • (1+2,真的):正如MDN所说,instanceof操作符测试对象(
    printer
    )的原型链(旧的
    PrintStuff.prototype
    )中是否有构造函数的原型属性(新的
    PrintStuff.prototype
    ,或
    {}
    )。“由于两者明显不同,
    instanceof
    返回
    false

  • instanceof
    是运算符,而不是方法。你写的是
    1+(2)

  • printsuff.prototype
    不是
    printsuff
    的原型;它是由
    PrintStuff
    构造函数创建的对象的原型。替换它时,在此之后创建的任何对象将不再具有
    .print
    方法<代码>打印机仍然如此,因为它仍然拥有旧的原型

  • (1+2,真的):正如MDN所说,instanceof操作符测试对象(
    printer
    )的原型链(旧的
    PrintStuff.prototype
    )中是否有构造函数的原型属性(新的
    PrintStuff.prototype
    ,或
    {}
    )。“由于两者明显不同,
    instanceof
    返回
    false

  • instanceof
    是运算符,而不是方法。你写的是
    1+(2)

  • printsuff.prototype
    不是
    printsuff
    的原型;它是由
    PrintStuff
    构造函数创建的对象的原型。替换它时,在此之后创建的任何对象将不再具有
    .print
    方法<代码>打印机仍然如此,因为它仍然拥有旧的原型

  • (1+2,真的):正如MDN所说,instanceof操作符测试对象(
    printer
    )的原型链(旧的
    PrintStuff.prototype
    )中是否有构造函数的原型属性(新的
    PrintStuff.prototype
    ,或
    {}
    )。“由于两者明显不同,
    instanceof
    返回
    false

  • instanceof
    是运算符,而不是方法。你写的是
    1+(2)

  • printsuff.prototype
    不是
    printsuff
    的原型;它是由
    PrintStuff
    构造函数创建的对象的原型。替换它时,在此之后创建的任何对象将不再具有
    .print
    方法<代码>打印机仍然如此,因为它仍然拥有旧的原型

  • (1+2,真的):正如MDN所说,instanceof操作符测试对象(
    printer
    )的原型链(旧的
    PrintStuff.prototype
    )中是否有构造函数的原型属性(新的
    PrintStuff.prototype
    ,或
    {}
    )。“由于两者明显不同,
    instanceof
    返回
    false


  • instanceof是JavaScript操作符-它检查被检查对象的原型链中是否存在函数(构造函数)的原型对象

    使用new创建对象时,javascript将对象的内部原型设置为链接到新函数的原型对象。当您将新的d函数更改为具有不同的原型对象时,原始创建的对象仍然链接到新的d函数的原始原型对象

    (在Chrome中),您可以访问对象的内部原型链接,因此可以通过执行
    PrintStuff.prototype=printer.\uuuuu proto\uuuu
    将其反转,如果这样可以更好地了解正在发生的事情

    你所说的“逆转”是什么意思?

    最初,创建PrintStuff函数时,PrintStuff对象链接到其原型,如下所示:

    [PrintStuff] --- prototype ---> [PrintStuffPrototype]
    
    执行此操作时:
    PrintStuff.prototype={}
    您将得到:

    [PrintStuff] -link lost- [PrintStuffPrototype]
           `.
             `---- prototype ---> {}
    

    PrintStuffPrototype对象挂起在内存中。反转它意味着将原始PrintStuff prototype重新链接到PrintStuff函数。

    instanceof是JavaScript运算符-它检查被检查对象的prototype链中是否存在函数(构造函数)的prototype对象

    使用new创建对象时,javascript将对象的内部原型设置为链接到新函数的原型对象。当您将新的d函数更改为具有不同的原型对象时,原始创建的对象仍然链接到新的d函数的原始原型对象

    (在Chrome中),您可以访问对象的内部原型链接,因此可以通过执行
    PrintStuff.prototype=printer.\uuuuu proto\uuuu
    将其反转,如果这样可以更好地了解正在发生的事情

    你所说的“逆转”是什么意思?

    最初,创建PrintStuff函数时,PrintStuff对象链接到其原型,如下所示:

    [PrintStuff] --- prototype ---> [PrintStuffPrototype]
    
    执行此操作时:
    PrintStuff.prototype={}
    您将得到:

    [PrintStuff] -link lost- [PrintStuffPrototype]
           `.
             `---- prototype ---> {}
    

    PrintStuffPrototype对象挂起在内存中。反转它意味着将原始PrintStuff prototype重新链接到PrintStuff函数。

    instanceof是JavaScript运算符-它检查prot中是否存在函数(构造函数)的prototype对象