标签:targe push 开发 pre 参数 cti es6 对象 实例
function Parent() { this.name = ‘kevin‘; } Parent.prototype.getName = function () { console.log(this.name); } function Child() { } Child.prototype = new Parent(); var child = new Child(); child.getName() //kevin
function Parent() { this.name = [‘kevin‘]; } function Child() { Parent.call(this); } Child.prototype = new Parent(); var child1 = new Child(); var child2 = new Child(); child1.name.push("cc");
function Parent() { this.name = [‘kevin‘]; } Parent.prototype.getName = function () { console.log(this.name, this.age); } function Child(age) { Parent.call(this); this.age = age; } Child.prototype = new Parent(); var child1 = new Child(19); var child2 = new Child(20); child1.name.push("cc"); child1.getName(); //["kevin", "cc"] 19
child2.getName(); //["kevin"] 20
function createObj(o) { function F() { } F.prototype = o; return new F(); }
function createObj(o) { var clone = Object.create(o); clone.say = () => { console.log(this) }; return clone } console.log(createObj({})); //{say: ƒ}
function Parent(name) { this.name = name; } Parent.prototype.getName = function () { console.log(this.name, this.age); } function Child(name, age) { Parent.call(this, name); this.age = age; } var F = function () { } F.prototype = Parent.prototype; Child.prototype = new F(); var child1 = new Child("cc", 20) console.log(child1) //Child {name: "cc", age: 20}
class Parent { constructor(x, y) { this.x = x; this.y = y } } class Child extends Parent { constructor(x, y, name) { super(x, y);//调用父类的constructor(x,y) this.name = name; } } var child1 = new Child("x", "y", "ccg"); console.log(child1); //Child {x: "x", y: "y", name: "ccg"}
class A { constructor() { console.log(new.target.name); } } class B extends A { constructor() { super(); } } new A(); new B();
class Parent { static myMethod(msg) { console.log(‘static‘, msg); } myMethod(msg) { console.log(‘instance‘, msg); } } class Child extends Parent { static myMethod(msg) { super.myMethod(msg); } myMethod(msg) { super.myMethod(msg); } } Child.myMethod(1); //static 1
var child = new Child(); child.myMethod(2); //instance 2
class Parent { constructor() { super(); console.log(super);//报错 } }
标签:targe push 开发 pre 参数 cti es6 对象 实例
原文地址:https://www.cnblogs.com/yaya-003/p/12876984.html