美文网首页
js的高级函数:apply call 与prototype

js的高级函数:apply call 与prototype

作者: 参观西湖 | 来源:发表于2019-12-20 14:11 被阅读0次

原有的对象没有特定的函数,怎么办?
比如:

function xx(f,l){
  this.firstName=f
  this.lastName=l
}
var person1=new xx("Bill","Gates")

第一种方法:apply
person1通过apply可以使用新的函数 fullName

var fullName=function() {
    return this.firstName + " " + this.lastName;
}

var x = fullName.apply(person1); 

第二种,call
person1通过 call 可以使用新的函数 fullName

var fullName=function() {
    return this.firstName + " " + this.lastName;
}

var x = fullName.call(person1); 

第三种,prototype
通过prototype 使得原有对象增加一个新的方法。

xx.prototype.fullName=function(){
    return this.firstName + " " + this.lastName;
}

var y=person1.fullName()

js的神奇之处。
js的无聊之处。

相关文章

网友评论

      本文标题:js的高级函数:apply call 与prototype

      本文链接:https://www.haomeiwen.com/subject/vnxinctx.html