简体   繁体   English

在JavaScript中处理继承时的isPrototypeOf()用法

[英]isPrototypeOf() usage when dealing with inheritance in JavaScript

//I have this base Rectangle constructor function
function Rectangle (length, width){
    this.length = length;
    this.width = width;
}

Rectangle.prototype.getArea = function (){
    return this.length * this.width;
};

//Creating Square constructor function that will inherit from Rectangle...

function Square(size){
    this.length = size;
    this.width  = size;
}

Square.prototype = new Rectangle(); 
Square.prototype.constructor = Square;

//creating rectangle and square instances
var rect = new Rectangle(5, 10);
var square = new Square(6);

console.log(rect.getArea());  //50
console.log(square.getArea());  //36 

console.log(Rectangle.prototype.isPrototypeOf(Square.prototype)); //true

console.log(Rectangle.prototype.isPrototypeOf(rect)); //true

console.log(Square.prototype.isPrototypeOf(square));  //true

my question is when i do the below console.log() , I expected it to print false . 我的问题是,当我执行下面的console.log() ,我希望它打印为false However, I got true . 但是,我得到了true

console.log(Rectangle.prototype.isPrototypeOf(square));  //true

1) Does this mean isPrototypeOf goes to multiple levels? 1)这是否意味着isPrototypeOf进入多个级别?

2) if isPrototypeOf goes multiple levels, what is the point of using isPrototypeOf instead of using instanceof ? 2)如果isPrototypeOf变为多个级别,使用isPrototypeOf而不是使用instanceof什么意义?

I've read this Why do we need the isPrototypeOf at all? 我读过这个为什么我们需要isPrototypeOf呢? but didn't understand how it applies in my use-case. 但不明白它在我的用例中是如何应用的。

  1. isPrototypeOf checks if an object is within another object's prototype chain so yes it does go multiple levels isPrototypeOf检查一个对象是否在另一个对象的原型链中,所以它确实是多个级别

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf

  1. They can be used for the same purpose. 它们可以用于相同的目的。 You can do square instanceof Square or Square.prototype.isPrototypeOf(square) but as you can see, instanceof has a specific purpose to match objects with their constructors where as isPrototypeOf can be used more broadly to check if any object is within the prototype chain of another. 您可以使用square instanceof SquareSquare.prototype.isPrototypeOf(square) square instanceof Square但正如您所看到的, instanceof具有将对象与其构造函数匹配的特定目的,其中isPrototypeOf可以更广泛地用于检查是否有任何对象在原型链中另一个

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

The isPrototypeOf() method tests for an object in another object's prototype chain isPrototypeOf()方法测试另一个对象的原型链中的对象

Here in Your Code 在您的代码中

console.log(Rectangle.prototype.isPrototypeOf(square)); // true

It prints true because Square Method is in the the chain of getArea Method and the getArea is the prototype method of Rectangle 它打印为true,因为Square Method位于getArea方法的链中,而getArea是Rectangle的原型方法

Rectangle.prototype.getArea = function (){
    return this.length * this.width;
};

For Example According to Mozilla Docs 例如根据Mozilla Docs

 function Fee() {
  // ...
}

function Fi() {
  // ...
}
Fi.prototype = new Fee();

function Fo() {
  // ...
}
Fo.prototype = new Fi();

function Fum() {
  // ...
}
Fum.prototype = new Fo()

Later on down the road, if you instantiate Fum and need to check if Fi's prototype exists within the Fum prototype chain, you could do this: 稍后,如果您实例化Fum并需要检查Fum原型链中是否存在Fi的原型,您可以这样做:

var fum = new Fum();
// ...

if (Fi.prototype.isPrototypeOf(fum)) {
  // do something safe
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM