call和apply區別 bind的使用

2021-10-24 08:59:23 字數 1026 閱讀 2750

//建構函式

function user(name)

// let obj=new user('yang');

// console.log(obj);

let unameobj=

user.call(unameobj,'趙四_劉能——多行不義必自斃');

console.log(unameobj);/* */

//建構函式的繼承

function requestfn()=$`).join('&')

}}function getuser()

function getarticle()

let a=new getuser();

let b=new getarticle;

console.log(a.get());

console.log(b.get());

3.bind的使用:

//bind使用

function requestfn(name,age)

let func=requestfn.bind(,'yang');//不立即執行

console.log(func('zhangsan',12))/*

yang zhangsan

dd.html:57 baobao.com */

/**引數是先從bind函式後面開始找 如果找到了就傳參 和呼叫的時候鏈結起來 yang第乙個引數,zhangsan 是第二個引數

*引數在**傳遞都行 可以呼叫的時候可以bind定義的時候 都有以bind定義時候為準

*/

*bind不立即執行 只是定義返回了新的函式

*引數是先從bind函式後面開始找 如果找到了就傳參  和呼叫的時候鏈結起來  yang第乙個引數,zhangsan  是第二個引數

*引數在**傳遞都行  可以呼叫的時候可以bind定義的時候 都有以bind定義時候為準

call 和apply 的區別

call方法 語法 call thisobj,object 定義 呼叫乙個物件的乙個方法,以另乙個物件替換當前物件 說明 call方法可以用來代替另乙個物件呼叫乙個方法。call方法可將乙個函式的物件上下文從初始的上下文改變為由thisobjext指定的新物件。如果沒有提供thisobj引數,那麼g...

call和apply的區別

1 方法定義 call方法 語法 call thisobj arg1 arg2 argn 定義 呼叫乙個物件的乙個方法,以另乙個物件替換當前物件。說明 call 方法可以用來代替另乙個物件呼叫乙個方法。call 方法可將乙個函式的物件上下文從初始的上下文改變為由 thisobj 指定的新物件。如果沒...

call和apply的區別

如下 複製 function add a,b function reduce a,b add.call reduce,1,3 將add方法運用到reduce,結果為4 如下例 如下 複製 function b b window b.call window b.call a 2,3 a 再看乙個複雜的...