![](/img/trans.png)
[英]how to pass variable from one function to another function in javascript
[英]How to make one variable a mathematical function of another variable in javascript
我的意思是數學意義上的標題中的問題。 給定變量 x 和 y,它們可以通過方程 y=f(x) 關聯,其中 f 是一些數學 function。 這意味着當 x=2 時,y=f(2),當 x=-5 時,y=f(-5),依此類推。 在數學環境中這是一件很自然的事情,我希望在 javascript 中有一種簡單的方法可以實現這種行為。 具體來說,讓我們想象一下 function f(x)=2x。 我們可以在代碼中實現這一點
function f(x){
return 2*x
}
但以下行為是默認行為
x=3
y=f(x)
y // returns 6
x=4
y // still returns 6
即使我們最初寫了 y=f(x),這種關系也不會隨着 x 的變化而持續存在。
我想這個問題的一個更簡單的版本是當 f 只是身份 function 並且我們使用 y=x 時。 你可以寫 y=x,改變 x,並且 y 仍然是 x 的舊值,這取決於 x 是否持有原始值或參考值以及我們如何更改 x 的值。
似乎為了復制一個值以使關系 y=x 持續存在,我們必須(1)通過引用復制它和(2)通過 point.xCoord = 6 更改值,以免弄亂引用信息。 我想知道是否有這樣的解決方案通常適用於 y=f(x) 而不僅僅是 y=x。
我現在使用的解決方法是將 x 存儲在 object 中,該 object 還跟蹤依賴於它的值,並且僅通過調用也更新依賴於 x 的每個值的方法來更新 x。 這感覺像是一個丑陋的解決方案,所以我想知道是否有更好的方法。
您要問的根本不是 JavaScript 變量如何工作。 從長遠來看,我認為您可能會從研究函數式編程概念和使用不可變變量中受益。
但是,可以通過在 object 方法中封裝 function 來創建類似於您描述的內容,特別是valueOf
和toString
方法,它們在 Z686155AF75A60A0F36E9D80C1FEZED 中具有特殊含義。
例如:
const track = (f) => ({ valueOf: f, toString: () => f().toString() }); let x = 7; let y = 8; const z = track(() => x * x + 2 * y); // further derive a value using z const a = track(() => z - x); console.log(`*** x = ${x}, y = ${y} ***`); console.log('z =', z.valueOf()); // valueOf() is implicitly evaluated if the variable is used // in a mathematical expression console.log('z * 2 =', z * 2); console.log('a =', a * 1); // coerce to a string value console.log(`z = ${z}, a = ${a}`); // change xx = 5; console.log(`*** x = ${x}, y = ${y} ***`); console.log('z * 2 =', z * 2); console.log('a =', a * 1); // change yy = 50; console.log(`*** x = ${x}, y = ${y} ***`); console.log('z * 2 =', z * 2); console.log('a =', a * 1); // compare using valueOf() const equals = (l, r) => l.valueOf() === r.valueOf(); console.log('equals(a, 120)', equals(a, 120)); console.log('equals(a, 125)', equals(a, 125));
更新:這個答案會對您的情況有所幫助嗎?
樣本:
function Model() { this.varX = ko.observable(1); this.varY = ko.computed(function() { return this.varX(); }, this); } var model = new Model(); setInterval(function() { model.varX(model.varX() + 1); console.log("x is updated ", model.varX()); console.log("y value ", model.varY()); }, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
JavaScript !== Math
。時期。
JavaScript 有點瘋狂的靈活的語言,所以在技術上是有辦法做到的。
但是來吧——JS 不是數學。 如果您的任何開發人員同事看到您想出的瘋狂工作方法,他們會在再次與您合作之前介意兩次。
那么該怎么辦?
如評論中所述,只需將 function 存儲在y
中。
const f = x => 2*x //This is an alternative declaration of `f`, that does the same
// vv--- no arguments, this will use the x from the outside.
const y = () => f(x)
//^^^--- const, because we won't expect y itself to change, but rather just the value returned by it
let x = 3
// call it ---vv
console.log(x, y()) //y() === 6, fine!
x = 2
console.log(x, y()) //y() === 4!
您不能使用它來定義其他功能的原因根本不存在!
//...
// vv--- call it, as always
const z = () => y() + 1
console.log(x, y(), z()) //z() === 5!
試試看:
const f = x => 2*x //This is an alternative declaration of `f`, that does the same // vv--- no arguments, this will use the x from the outside. const y = () => f(x) //^^^--- const, because we won't expect y itself to change, but rather just the value returned by it let x = 3 // call it ---vv console.log('x =', x, 'y =', y()) //y() === 6, fine. x = 2 console,log('x =', x, 'y =', y()) //y() === 4. // vv--- call it, as always const z = () => y() + 1 console,log('x =', x, 'y =', y(). 'z =', z()) //z() === 5, x = 42 console,log('x =', x, 'y =', y(), 'z =', z()) //x = 42, y = 84, z = 85
我無法在您的代碼中找到錯誤,但我會嘗試重寫它
function f(x) { return (x*2); } var x = 2; var y = f(x); console.log(y); x = 3; y = f(x); console.log(y); //If you want to store the output then, y = f(2); or y = f(3)
嘗試運行上面的代碼片段。 這個對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.