function Grand(){}; Grand.prototype.name="grand"; let grand = new Grand(); Father.prototype=grand; function Father(){} let father = new Father(); Son.prototype=father; function Son(){} let son = new Son();
function Father() { } function Son() { Father.call(this); // 调用父类构造函数 } Son.prototype.print = function() { Father.prototype.print.call(this);//只使用单个方法 } // 子类继承父类的原型 Son.prototype = Object.create(Father.prototype); Son.prototype.constructor = Son;
let inherit =(function(){ let Interim =function Interim() {}; return function (Target,Origin){//继承源 Interim.prototype =Object.create(Origin); Target.prototype = Interim.prototype; //如今 能够 制定本身的私有属性,可是 constuctor 不是 原函数所一手动赋值回来,若是想要知道函数真正继承那个原型须要保存它 Target.prototype.constuctor = Target; Target.prototype.yliluokka =Origin; } }())
function Fn1(){ } function Fn2(){ } function Son(){ F1.call(this); F2.call(this); } //继承F1 Son.prototype =Object.create(Fn1.prototype); //继承F2 Object.assign(Son.prototype,Fn2.prototype); Son.prototype.constructor =Son; let a =new Son();
function test() {} //test() == test.call() let obj ={}; Object.prototype.toString.call(obj) //"[object Object]" //由于call 和 apply 会将函数中的this指向第一个参数 //至关于 obj.toString()
//返回数组中最大的数 let a = [1, 2, 4, 1, 15]; Math.max.apply(null, a) // 15 //将数组的空元素变为undefined Array.apply(null [1,,3,,4)//[1,undefined,3,undefined,4];
let obj={0: 1, length: 2} Array.protetype.slice.apply(obj);//[1,undefined]
let counter = { count: 0, inc: function () { this.count++; } }; let func = counter.inc.bind(counter); func(); counter.count // 1 let add = function (x, y) { return x * this.m + y * this.n; } let obj = { m: 2, n: 2 }; let newAdd = add.bind(obj, 5); //将x 绑定为 5 newAdd(5) // 20 newAdd(1,5)//12
第一个参数是null或undefined,等于将this绑定到全局对象html
let counter = { count: 0, inc: function () { 'use strict'; this.count++; } }; function callIt(callback) { callback(); } callIt(counter.inc.bind(counter)); counter.count // 1
[1, 2, 3].slice(0, 1) // [1] // 等同于 Array.prototype.slice.call([1, 2, 3], 0, 1) // [1] //将Array.prototype.slice变成Function.prototype.call方法所在的对象 //调用时就变成了Array.prototype.slice.call。 let slice = Function.prototype.call.bind(Array.prototype.slice); Function.prototype.slice.call([1, 2, 3], 0, 1) // [1] //slice([1, 2, 3], 0, 1) let push = Function.prototype.call.bind(Array.prototype.push); let pop = Function.prototype.call.bind(Array.prototype.pop); let a = [1 ,2 ,3]; push(a, 4) a // [1, 2, 3, 4] pop(a) a // [1, 2, 3]
function f() { console.log(this.v); } let o = { v: 123 }; let bind = Function.prototype.call.bind(Function.prototype.bind); bind(f, o)() // 123
function Foo (){} let obj = new Foo (); Object.getPrototypeOf(obj) // Foo.prototype //空对象原型 Object.getPrototypeOf({}) // Object.prototype // Object.prototype 原型 Object.getPrototypeOf(Object.prototype) //null // Foo Object.getPrototypeOf(Foo) // Function.prototype
let now = {}; let pro = {name:"Owen"}; Object.setPrototypeOf(now,pro); now.name //"Owen"
let obj = Object.create(null); obj.toString()// Error //会继承第二个参数的属性和方法 let obj = Object.create({}, { p1: { value: 123, enumerable: true, configurable: true, writable: true, }, p2: { value: 'Owen', enumerable: true, configurable: true, writable: true, } }); // 等同于 let obj = Object.create({}); obj.p1 = 123; obj.p2 = 'Owen'; //生成的对象会继承它的原型对象的构造函数。 function Foo() {} let f = new Foo(); let b = Object.create(f); b.constructor === Foo // true b instanceof Foo // true
function F(){} let f = new F() F.prototype.isPrototypeOf(f) //true
//无论可不可遍历都会返回出来 Object.getOwnPropertyNames(Date);//["length", "name", "prototype", "now", "parse", "UTC"] //返回可遍历属性 Object.keys(Date)// []
Array.hasOwnProperty('length')//true
function copyOwn (target,origin){ Object.getOwnPropertyNames(origin).forEach((key)=>{ let desc =Object.getOwnPropertyDescriptor(origin,key); Object.defineProperty(target,origin,desc); }) return target } function copy(origin){ let clone = Object.create (Object.getPrototypeOf(origin)); copyOwn(clone,origin) return clone } //es8 const copyTwo = origin =>Object.create( Object.getPropertyOf(origin),Object.getOwnPropertyDescriptor(origin) );
oop(一)es6