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