Prototype, prototype chain, judging data type

Table of contents

effect

prototype chain

Reference type: __proto__ (implicit prototype) attribute, the attribute value is the object function: prototype (prototype) attribute, the attribute value is the object

Function: It is also a function itself

Related methods

person.prototype.isPrototypeOf(stu)

Object.getPrototypeOf(object) replaces the deprecated object._ _ proto _ _ 

Object.create(instance) creates a new object using an existing object as a prototype

Object.hasOwn(obj,property): Determine whether it is its own property

application

new

Function decorator: add additional logic before or after a function is executed

Determine data type

operator

typeof: determine basic data type

typeof null=Object type labels are all 000

Instance instanceof constructor: determine prototype chain, and isPrototypeOf

method

Constructor.prototype.isPrototypeOf(instance): Determine the prototype chain

(data).constructor === Data type: does not include inherited types

Display: toString, valueOf except null, undefined

valueOf: this value is converted into an object. Except for Date, the data itself is returned.

console.log

toString: Override the type conversion of the object. console.log


effect

A prototype is defined as an object that provides shared properties to other objects . Instances of functions can share properties and methods on the prototype.

prototype chain

引用类型:__proto__(隐式原型)Properties, property values ​​are object
函数:prototype(原型) properties, property values ​​are objects

const o = {
  a: 1,
  b: 2,
  // __proto__ 设置了 [[Prototype]]。它在这里被指定为另一个对象字面量。
  __proto__: {
    b: 3,
    c: 4,
    __proto__: {
      d: 5,
    },
  },
};

// { a: 1, b: 2 } ---> { b: 3, c: 4 } ---> { d: 5 } ---> Object.prototype ---> null

console.log(o.d); // 5

Function: It is also a function itself

console.log(Function.prototype === Function.__proto__); // true

Function itself is a function. All functions are instances of Function. So Function is an instance of Function

Related methods

person.prototype.isPrototypeOf(stu)

Object.getPrototypeOf( object ) replaces the deprecated object._ _ proto _ _ 

Object.create(instance) creates a new object using an existing object as a prototype

Object.hasOwn(obj,property): Determine whether it is its own property

application

new

var a=1;
function fn1(){
  var a=2;
  console.log(this.a+a);
}
//f1并没有被作为对象的方法调用, this 指向全局对象,在浏览器中是 window
f1();//3 

function fn2(){
  var a=10;
  fn1();
}
//在 fn2 函数内部调用 fn1 函数,但是 fn1 函数内部的 this 仍然指向全局对象 window,因为 fn1 并没有被作为方法调用。
fn2();//3 

var fn3=function(){
  this.a=3;
}
fn3.prototype={
  a:4
}
var fn33=new fn3();
fn1.call(fn33)
//5

Function decorator: add additional logic before or after a function is executed

Function.prototype.before=function(beforefn){
  return ()=>{
    beforefn.apply(this,arguments)
    return this.apply(this,arguments)
  }
}
Function.prototype.after=function(afterfn){
  return ()=>{
    var res=this.apply(this,arguments)
    afterfn.apply(this,arguments)
    return res;
  }
}

var func=function(){
  console.log(1)
}.before(function(){
  console.log(2)
}).after(function(){
  console.log(3)
})
func()//213

Determine data type

operator

typeof: determine basic data type

typeof null=Object type labels are all 000

实例 instanceof 构造函数: determine the prototype chain, andisPrototypeOf

Object.prototype.isPrototypeOf({})// true
{} instanceof Object// true
Object instanceof Object//true
Function instanceof Function//true

method

构造函数.prototype.: isPrototypeOf(实例) Determine the prototype chain

( data ).constructor === Data type : does not contain inherited types

Display: toString, valueOf except null, undefined

valueOf: Convert this value to object . Except for Date , the data itself is returned.

console.log

Insert image description here

toString: Override the type conversion of the object . console.log

Insert image description hereInsert image description here

おすすめ

転載: blog.csdn.net/qq_28838891/article/details/133136691
おすすめ