typeof
typeof 操作符返回一个字符串,表示未经计算的操作数的类型
typeof null === 'object'
instanceof
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上
function Car(make) {
this.make = make;
}
const auto = new Car('Honda');
console.log(auto instanceof Car);
// expected output: true
console.log(auto instanceof Object);
// expected output: true
实现原理
function instance_Of(L,R){
L = L.__proto__
R = R.prototype
while(true){
if(L === R) return true
if(L === null) return false
L = L.__proto__
}
}
Object.create()
Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的** _ _ proto _ _ **
const person = {
isHuman: false,
printIntroduction: function() {
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
}
};
const me = Object.create(person);
me.name = 'Matthew'; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // inherited properties can be overwritten
me.printIntroduction();
// expected output: "My name is Matthew. Am I human? true"
me.__proto__ === person //true
Object.create实现类式继承
// Shape - 父类(superclass)
function Shape() {
this.x = 0;
}
// 父类的方法
Shape.prototype.move = function(x) {
this.x += x;
};
// Rectangle - 子类(subclass)
function Rectangle() {
Shape.call(this); // call super constructor.
}
// 子类续承父类
Rectangle.prototype = Object.create(Shape.prototype);
//Rectangle.prototype = new Shape()也可以创建一个关联到Shape的对象,但是它使用了构造函数调用,如果函数存在副作用,则会产生你不希望的影响
//副作用会覆盖Rectangle.prototype,要重写Rectangle.prototype.constructor
Rectangle.prototype.constructor = Rectangle;
Object.create原理
Object.create = Object.create || function(o){
function Fn(){}
Fn.prototype = o
return new Fn()
}
Object.setPrototypeOf()
Object.setPrototypeOf() 方法设置一个指定的对象的原型 ( 即, 内部[[Prototype]]属性)到另一个对象或 null
// Shape - 父类(superclass)
function Shape() {
this.x = 0;
}
// Rectangle - 子类(subclass)
function Rectangle() {
Shape.call(this); // call super constructor.
}
// 子类续承父类
//(标准化__proto__ ) Rectangle.prototype.__proto__ = Shape.prototype
Object.setPrototypeOf(Rectangle.prototype, Shape.prototype)
Object.getPrototypeOf()
Object.getPrototypeOf() 方法返回指定对象的原型(内部[[Prototype]]属性的值)。
function Shape() {
this.x = 0;
}
// Rectangle - 子类(subclass)
function Rectangle() {
Shape.call(this); // call super constructor.
}
Object.setPrototypeOf(Rectangle.prototype, Shape.prototype)
const r = new Rectangle()
console.log(r.__proto__ === Rectangle.prototype) //true
console.log(Rectangle.prototype.__proto__ === Shape.prototype) //true
console.log(Object.getPrototypeOf(r) === Rectangle.prototype) //true
console.log(Object.getPrototypeOf(Rectangle.prototype) === Shape.prototype)//true
Object.prototype.isPrototypeOf()
isPrototypeOf() 方法用于测试一个对象是否存在于另一个对象的原型链上。
function Foo() {}
function Bar() {}
function Baz() {}
Bar.prototype = Object.create(Foo.prototype);
Baz.prototype = Object.create(Bar.prototype);
var baz = new Baz();
console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true
Object.is
Object.is用来比较两个值是否严格相等,与严格比较运算符(===)的行为基本一致,同之处只有两个:一是+0不等于-0,二是NaN等于自身
+0 === -0 //true
NaN === NaN // false
Object.is(+0, -0) // false
Object.is(NaN, NaN) // true
Function.prototype.call
Function.prototype.call = function (ctx, ...args) {
ctx = ctx || window
const fn = Symbol()
ctx[fn] = this
const res = ctx[fn](...args)
delete ctx[fn]
console.log(res)
return res
}
//或者
Function.prototype.call = function (ctx) {
ctx = ctx || window
const fn = Symbol()
ctx[fn] = this
const res = ctx[fn](...Array.from(arguments).slice(1))
delete ctx[fn]
return res
}
Function.prototype.apply
Function.prototype.apply = function (ctx, args) {
ctx = ctx || window
const fn = Symbol()
ctx[fn] = this
const res = ctx[fn](...args)
delete ctx[fn]
console.log(res)
return res
}
Function.prototype.bind
Function.prototype.bind = function (ctx, ...args) {
const self = this
return function(){
ctx = ctx || window
const fn = Symbol()
ctx[fn] = self
const res = ctx[fn](...args)
delete ctx[fn]
console.log(res)
return res
}
}