javascript call apply

來源:互聯網
上載者:User

標籤:

call :

call 方法可以用來代替另一個對象調用一個方法。call 方法可將一個函數的物件內容從初始的上下文改變為由 thisObj 指定的新對象。 
  如果沒有提供 thisObj 參數,那麼 Global 對象被用作thisObj。說明白一點其實就是更改對象的內部指標,即改變對象的this指向的內容。這在物件導向的js編程過程中有時是很有用的。

apply:

應用某一對象的一個方法,用另一個對象替換當前對象。 

如果沒有提供 argArray 和 thisObj 任何一個參數,那麼 Global 對象將被用作 thisObj, 並且無法被傳遞任何參數。

 

 

function Animal(xx){

this.name=‘zhu‘
this.showName=function (cc,jj){
alert(this.name+cc+‘,‘+jj);


}
alert(this.name+xx);
}
function Cat()
{
this.name=‘cat‘;

}
//var animal=new Animal();
//var cat=new Cat();
Animal.call(Cat,‘df‘);
//this.name;

Animal.apply();
//this.name;

 

 

var zhu=function(name){this.name=name};
var ben=function(sex){this.sex=sex;}
var xi=function(age){this.age=age;}
var people=function(name,sex,age,score){zhu.call(this,name);ben.call(this,sex);xi.call(this,age);this.score=score;}
people.prototype.construction=people;
var p=new people(‘zhao‘,‘male‘,‘11‘,‘78‘);
console.log(p.name);
console.log(p.sex);
console.log(p.age);
console.log(p.score);

 

 

function Animal(name)
{
this.name=name;
this.showName=function(){alert(this.name);}
}
function Cat(name)
{
Animal.call(this,name);
}
var cat=new Cat(‘White cat‘);
cat.showName();

 

 

function Animal(){

this.name=‘Animal‘;
this.showName=function(aa){
alert(this.name+aa);
}
}
function Cat()
{
this.name=‘Cat‘;
}
var animal=new Animal();
var cat=new Cat();
animal.showName.call(cat,‘kk‘);

 

 

function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub,4,9);

 

 

function base(){this.member="a people";this.method=function(){window.alert(this.member);}}
function extend()
{
base.call(this);

window.alert(member);
method();
window.alert(this.method);
}
extend();

 

var ClassA={create:function(){ return function(){ this.cc.apply(this,arguments);this.ee.apply(this,arguments); } }} var vehicle=ClassA.create(); vehicle.prototype={ee:function(uu,kk){alert("0")} ,cc:function(type,xx){this.type=type;this.xx=xx;},showSelf:function(){ alert(this.xx+"this +vehile"+this.type);}} var moto=new vehicle("Moto","lll"); moto.showSelf();

 

var a="jjj"; var b="abc";var func=new function(){this.a="func";this.b="bbb";} var myfunc=function(x){ var a="myfunc";this.b="kkk"; alert(this.a); alert(this.b); }; myfunc.call(func,"var");

javascript call apply

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.