方法:javascript
var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } Person.prototype = o; var p = new Person(); console.log( p.hasOwnProperty( 'name' ) ); // false 這個 name 屬性不是本身提供的 console.log( p.hasOwnProperty( 'age' ) ); // true 這個 nage 屬性是本身提供的 //for in 遍歷對象時 有時但願遍歷的是對象本身提供的成員而不是從原型那裏繼承的 for(var k in p){ if(p.hasOwnProperty(k)){ console.log(k); } }
var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } Person.prototype = o; var p = new Person(); console.log( p.hasOwnProperty( 'name' ) ); // false 這個 name 屬性不是本身提供的 console.log( p.hasOwnProperty( 'age' ) ); // true 這個 nage 屬性是本身提供的 //for in 遍歷對象時 有時但願遍歷的是對象本身提供的成員而不是從原型那裏繼承的 for(var k in p){ if(p.hasOwnProperty(k)){ console.log(k); } }
var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } var p1 = new Person(); Person.prototype = o; var p2 = new Person(); console.log( o.isPrototypeOf( p1 ) ); //false o 不是 p1 的原型 console.log( o.isPrototypeOf( p2 ) );//true o 是 p2 的原型 console.log( Object.prototype.isPrototypeOf( p1 ) );//true Object.prototype 在 p1 的原型鏈上 console.log( Object.prototype.isPrototypeOf( p2 ) );//true Object.prototype 在 p2 的原型鏈上
var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } var p1 = new Person(); Person.prototype = o; var p2 = new Person(); console.log( o.isPrototypeOf( p1 ) ); //false o 不是 p1 的原型 console.log( o.isPrototypeOf( p2 ) );//true o 是 p2 的原型 console.log( Object.prototype.isPrototypeOf( p1 ) );//true Object.prototype 在 p1 的原型鏈上 console.log( Object.prototype.isPrototypeOf( p2 ) );//true Object.prototype 在 p2 的原型鏈上
方法: 1.apply call 用法:上下文調用模式,自定義設置this的含義java
語法: 函數名.apply(對象,[參數]) 函數名.call(對象,參數)程序員
描述:數組
函數名就是表示函數自己,使用函數進行調用的時候默認this 就是全局變量app
函數名是方法時,this是指當前對象函數
使用apply調用後,不管是函數仍是方法this都由apply的第一個參數決定ui
注意:this
若是函數或方法中沒有this的操做,不管什麼調用都同樣spa
若是是函數調用foo(),有點像foo.apply(window)prototype
若是是方法調用o.method(),有點像o.method.apply(o)
參數問題:若是函數或方法無參數時,用call或是apply都同樣
第一個參數的使用規則:
var func2 = function() { this.name = "程序員"; }; var o={name:'xjj'}; func2.apply(o); console.log(o.name);// 程序員
var func2 = function() { this.name = "程序員"; }; var o={name:'xjj'}; func2.apply(o); console.log(o.name);// 程序員
若是不傳參或傳入爲null undefined等 this默認爲window
若是傳入的是基本數據類型,this就指向這個基本類型對應的包裝類型的引用
第二個參數:在使用上下文調用時,若是原函數或方法有參數,那麼就使用第二個參數來表示
應用:上下文調用只是能修改this,可是最多使用的是借用函數調用
var a = {}; a[ 0 ] = 'a'; a[ 1 ] = 'b'; a.length = 2; var arr=[]; var newArr=arr.push.apply(arr,a); push 返回所加元素的個數,newArr=2;arr=["a", "b"] a是一個僞數組,沒法使用數組的方法, 處理數組轉換,能夠使用apply將數組或僞數組展開的特性,將元素一個一個取出來構成一個新數組
var a = {}; a[ 0 ] = 'a'; a[ 1 ] = 'b'; a.length = 2; var arr=[]; var newArr=arr.push.apply(arr,a); push 返回所加元素的個數,newArr=2;arr=["a", "b"] a是一個僞數組,沒法使用數組的方法, 處理數組轉換,能夠使用apply將數組或僞數組展開的特性,將元素一個一個取出來構成一個新數組
在來一個例子:
var a = { length: 0 }; a[ a.length++ ] = 'abc'; a[ a.length++ ] = 'def'; var arr=[]; var newArr=arr.slice.apply(a,[0]); 結果爲: newArr=["abc", "def"]; slice語法: arr.slice( index, endIndex ) 從index開始,endIndex取不到 返回的是截取到元素 若是第二個參數不傳, 那麼就是 從 index 一致獲取到結尾,該方法不會修改原數組
var a = { length: 0 }; a[ a.length++ ] = 'abc'; a[ a.length++ ] = 'def'; var arr=[]; var newArr=arr.slice.apply(a,[0]); 結果爲: newArr=["abc", "def"]; slice語法: arr.slice( index, endIndex ) 從index開始,endIndex取不到 返回的是截取到元素 若是第二個參數不傳, 那麼就是 從 index 一致獲取到結尾,該方法不會修改原數組
2.caller 概念:通常不推薦使用. 得到函數的調用者.
3.bind 概念:綁定, 這個語法來源於 ES5
var f = document.getElementById; f( 'id' ); //這樣會報錯 非法調用 f.call( document, 'id' ); 或者換一種方式: var f = document.getElementById.bind( document ); f( 'id' ); //這樣不會報錯
var f = document.getElementById; f( 'id' ); //這樣會報錯 非法調用 f.call( document, 'id' ); 或者換一種方式: var f = document.getElementById.bind( document ); f( 'id' ); //這樣不會報錯
instanceof 運算符 概念:判斷對象是否是由指定構造方法所建立或者說構造函數的原型屬性是否在對象所在的原型鏈上. 語法: 對象 instanceof 函數 描述: '函數.prototype' 是否在 '對象' 的原型鏈上
function Person() {} var p = new Person(); console.log( p instanceof Person );//true
function Person() {} var p = new Person(); console.log( p instanceof Person );//true
function Person(){}; var p1=new Person(); Person.prototype={}; var p2=new Person(); console.log(p1 instanceof Person); //false Person.prototype不在 p1 的原型鏈上 console.log(p2 instanceof Person); //true Person.prototype在 p2 的原型鏈上
function Person(){}; var p1=new Person(); Person.prototype={}; var p2=new Person(); console.log(p1 instanceof Person); //false Person.prototype不在 p1 的原型鏈上 console.log(p2 instanceof Person); //true Person.prototype在 p2 的原型鏈上