__proto__ prototype

实例化对象的原型(i.e __proto__)指向了构造函数的prototype属性
array.__proto__  ==== Function.prototype;
array.__proto__ .__proto__ ==== Function.prototype.__proto__ === Object.prototype

ii

function Person(name){
  this.name= name;
}
Person.prototype.sayHi = function(){
  console.log(`hello I'm ${this.name}`);
};
let mike = new Person('mike')
let mike2 = new Person('mike2')

mike.sayHi();
mike2.sayHi();

console.log(mike.sayHi === mike2.sayHi) //true
function Person(name){
  this.name= name;
  this.sayHi = function(){
   console.log(`hello I'm ${this.name}`);
  }
}

let mike = new Person('mike')
let mike2 = new Person('mike2')

mike.sayHi();
mike2.sayHi();
console.log(mike.sayHi === mike2.sayHi) //false

instanceof

var arr = []
console.log(arr instanceof Array)// true

var arr = []
console.log(arr instanceof Object)// true
实现一个函数

实例化对象的原型(i.e proto)指向了构造函数的prototype属性

//arr.__proto__ === fn.prototype
arr.__proto__ === 
一:
function io(ins,fn){
  while (ins.__proto__ !== null) {
    if (ins.__proto__ === fn.prototype) {
      return true
    }
    ins = ins.__proto__
  }
  return false
}
console.log(io(arr,Array));
console.log(io(arr,Object));
console.log(io(arr,Number));
二:
// instanceof

let arr = [1, 2];


// arr.__proto__ === fn.prototype
// arr.__proto__.__proto__ === fn.prototype
// arr.__proto__.__proto__.__proto__ === fn.prototype
// null

function io(arr, fn) {
  if (arr.__proto__) {
    if (arr.__proto__ === fn.prototype) {
      return true;
    } else {
      return io(arr.__proto__, fn);
    }
  } else {
    return false;
  }
}

console.log(io(arr, Array)); //true
console.log(io(arr, Object)); //true
console.log(io(arr, Number)); //false


function Person(name) {
  this.name = name;
}

Person.prototype.sayHi = function () {
  console.log(`hello I'm ${this.name}`);
};

let mike = new Person('mike');
let mike2 = new Person('mike2');

mike.sayHi = function () {
  console.log(`hi I'm ${this.name}`);
};

console.log(io(mike, Person)) // true;


你可能感兴趣的:(__proto__ prototype)