吴华锦

功能代码集合

2023-01-24

JS原型链

function Person() {}
Person.prototype.name = "abc";
Person.prototype.sayName = function () {
console.log(this.name);
};

let person = new Person();

person.__proto__ = Person.prototype;
String.__proto__ = Function.prototype;
String.constructor = Function;

JS继承

原型继承

function Parent() {
this.name = "Parent";
this.sex = "male";
}

function Child() {
this.name = "Child";
}

Child.prototype = new Parent();

构造函数继承

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

function Child() {
Parent.call(this, "Child");
}

组合继承

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

function Child(name) {
Person.call(this, name);
}

Child.prototype = new Persion();
Child.prototype.constructor = Child;

new关键字

function _new(constructor, ...arg) {
let obj = {};
obj.__proto__ = constructor.prototype;
let res = constructor.apply(obj, arg);
return Object.prototype.toString.call(res) === "[object Object]" ? res : obj;
}
Tags: 前端
使用微信打赏 使用支付宝打赏

雅致寓于高阁渔舟唱晚,古典悠然;
格调外发园林绿树萦绕,馥郁清香。

扫描二维码,分享此文章