亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何綁定參數,將函數分配給類原型

如何綁定參數,將函數分配給類原型

肥皂起泡泡 2022-10-21 11:04:50
假設我有一個功能function getSum (firstValue) { return firstValue + this.secondValue }和一些班級class TestClass {}我如何動態地將函數分配getSum給類原型,并將 firstValue 綁定為 1,所以之后// some code like TestClass.prototype.getSum = getSum.bind(null, 1)const obj = new TestClass()obj.secondValue = 2console.log(obj.getSum()) // 3我可以得到 3對于對象,我可以這樣做obj.getSum = getSum.bind(obj, 1)但是TestClass.prototype因為上下文還不存在所以我不能設置綁定的第一個參數這個難題可以直接解決嗎?間接地我可以做這樣的事情const firstValue = 1TestClass.getSum = function () {  return getSum.bind(this, firstValue)()}或者像這樣TestClass.firstValue = 1TestClass.getSum = function () {  return getSum.bind(this)(TestClass.firstValue)}但也許可以更直接地完成
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

您可以創建一個函數,該函數將調用一個參數,getSum但本身提供第一個參數。


TestClass.prototype.getSum = function() { //<–– normal function

  return getSum.call( this,   1 );

//              ^^^^  ^^^^    ^ 

//               ||    ||     |

// forward this –++––––++     +–––––– pass a value for the first parameter

}

這將為您提供以下信息


function getSum (firstValue) { return firstValue + this.secondValue }


class TestClass {}


TestClass.prototype.getSum = function() {

  return getSum.call(this, 1);

}


const obj = new TestClass()


obj.secondValue = 2

console.log(obj.getSum()) // 3


通常,將值綁定到函數中的參數的過程稱為部分應用程序。例如,如果一個函數需要三個參數,那么您一開始只能設置兩個,然后再設置最后一個。整個過程可以通過創建一個函數來處理這個抽象出來:


function partiallyApply(fn, ...params) {

  return function(...moreParams) {

    return fn.call(this, ...params, ...moreParams);

  }

}


function takes4Parameters (a, b, c, d)  {

  return a + b + c + d;

}


const takes2Parameters = partiallyApply(takes4Parameters, 1, 2); // 1 + 2 + c + d


console.log("1 + 2 + 11 + 12 =", takes2Parameters(11, 12));


const takes1Parameter = partiallyApply(takes2Parameters, 3); // 1 + 2 + 3 + d


console.log("1 + 2 + 3 + 5 =", takes1Parameter(5));


const takesNoParameter = partiallyApply(takes1Parameter, 6); // 1 + 2 + 3 + 6


console.log("1 + 2 + 3 + 6 =", takesNoParameter());


使用那個高階函數,我們可以更容易地推導getSum出TestClass


function getSum (firstValue) { return firstValue + this.secondValue }

function partiallyApply(fn, ...params) {

  return function (...moreParams) {

    return fn.call(this, ...params, ...moreParams)

  }

}


class TestClass {}


TestClass.prototype.getSum = partiallyApply(getSum, 1);


//example of adding other partially applied methods:

TestClass.prototype.getSum2 = partiallyApply(getSum, 2);

TestClass.prototype.getSum3 = partiallyApply(getSum, 3);

TestClass.prototype.getSum4 = partiallyApply(getSum, 4);


const obj = new TestClass()


obj.secondValue = 2

console.log(obj.getSum());  // 3


console.log(obj.getSum2()); // 4

console.log(obj.getSum3()); // 5

console.log(obj.getSum4()); // 6

查看完整回答
反對 回復 2022-10-21
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

<!DOCTYPE html>

<html>

<body>


<h2>JavaScript Objects</h2>


<p id="demo"></p>


<script>

function Sum(first, second) {

  this.firstValue = first;

  this.secondValue = second;

 

}

Sum.prototype.getSum = function() { return this.firstValue + this.secondValue }


var mysum = new Sum(50, 10);

document.getElementById("demo").innerHTML =

"Sum is" + mysum.getSum(); 

</script>


</body>

</html>


查看完整回答
反對 回復 2022-10-21
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

讓我知道這是否適合您。


function getSum(firstValue = 1) {

  return firstValue + this.secondValue

}


// or

//function getSum() {

//  const firstValue = arguments.length ? arguments[0] : 1;

//  return firstValue + this.secondValue

//}


class Test {}


Test.prototype.getSum = getSum;

// or

// Test.prototype["getSum"] = getSum;


// or

// const methodName = "getSum";

// Test.prototype[methodName] = getSum;


const test = new Test();

test.secondValue = 100;


console.log(test.getSum()) // -> 101, firstValue is 1

console.log(test.getSum(11)) // -> 111, firstValue is 11


查看完整回答
反對 回復 2022-10-21
  • 3 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號