js中call(),apply(),以及prototype的含義

最近段時間主要學習前端去了,然而所遇到的一些問題我以爲有必要去深究一下前端

prototype:數組

 1 js中有三種表達方法app

類方法,屬性方法,原型方法ide

function People(name) {
    this.name=name;
    //對象方法
    this.Introduce=function(){
        console.log("My name is "+this.name);
    }
}
//類方法
People.Run=function(){
    console.log("I can run");
}
//原型方法
People.prototype.IntroduceChinese=function(){
    console.log("個人名字是"+this.name);
}
//測試
var p1=new People("xx");
p1.Introduce(); //   My name is xx
People.Run();  //I can run
p1.IntroduceChinese(); 個人名字是xx

其實從上面能夠看出prototype,實際上向people中添加了一個方法,而這也應官方的解釋「prototype 屬性使您有能力向對象添加屬性和方法"學習

2 實現繼承測試

 

function baseClass(){
    this.showMessage = function () {
        console.log('baseClass:','woc this is bad boy')
    }
}

// function extendClass(){}

function extendClass(){
    this.showMessage = function () {
        console.log('extendClass:', 'woc this is good body')
    }
}

function extendClass1(){}


 extendClass.prototype = new baseClass()
extendClass1.prototype = new baseClass()

var eC = new extendClass() //extendClass: woc this is good body

var eC1 = new extendClass1() //baseClass: woc this is bad boy
eC.showMessage()
eC1.showMessage()

 從上面的案例能夠看出若是extendClass()的showMessage存在的狀況就會指向本身,若是不存在就會指向其」父類「this

 

call() 和 appyl()spa

1 每一個function中有一個prototype, call(), apply()prototype

call() apply() 我簡單的理解爲改變你當前對象的指向,這可能有點抽象,看下代碼指針

function method1(arg1, arg2) {
    return arg1+arg2
}

function method2(arg1, arg2) {
    return arg1-arg2
}

var result1 = method2.apply(method1,[3,2]);

var result2 = method1.call(method2,3,3)

console.log(result1); //1
console.log(result2); //6

 

 從上面的實例能夠看出兩個function的指向發上了改變

call() apply(): 這個是當前的this指針指向調用你的那個function(有點相似copy的意思)

而二者的區別在於apply() 在參數上只有兩個參數(當前方法,數組),

而call()的參數則是單個單個的形式

2 實現繼承

function father(word) {
    this.word = word
    this.showName1 = function(){
        console.log('Father say:', this.word)
    }
}

function mother(word) {
    this.word = word
    this.showName2 = function () {
        console.log('Mother say:', this.word)
    }
}

function child(word) {
    // father.apply(this,[word])
    father.call(this, word)
    mother.call(this, word)
}

var c = new child('boys');

c.showName1(); // Father say: boys
c.showName2(); // Mother say: boys

 

3 好的案例

(1)活用

var result = Math.max(7.25,7.30)

var array = [1,3,4,5,6,0,32.3,3.3]

var result1 = Math.max.apply(null,array);
var result2 = Math.min.apply(null,array);
console.log(result)  // 7.3
console.log(result1) // 32.3
console.log(result2)  // 0

 在js Math.max()中的參數是沒有傳數組的形式的,而這裏經過apply()巧妙地實現了這種轉變,首先咱們並不須要那個對象去指向Math,因此放了一個null作爲參數,而後將arary數組傳入其中

(2) 理解

function baseClass() {
    this.showMsg = function()
    {
        console.log("baseClass::showMsg");
    }

    this.baseShowMsg = function()
    {
        console.log("baseClass::baseShowMsg");
    }
}
baseClass.showMsg = function()
{
    console.log("baseClass::showMsg static");
}

function extendClass()
{
    this.showMsg =function ()
    {
        console.log("extendClass::showMsg");
    }
}
extendClass.showMsg = function()
{
    console.log("extendClass::showMsg static")
}

extendClass.prototype = new baseClass();
var instance = new extendClass();

instance.showMsg(); //顯示extendClass::showMsg
instance.baseShowMsg(); //顯示baseClass::baseShowMsg
instance.showMsg(); //顯示extendClass::showMsg

baseClass.showMsg.call(instance);//顯示baseClass::showMsg static

var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//顯示baseClass::showMsg
View Code
相關文章
相關標籤/搜索