2016-11-29 4 views
1

関数の事前バインド値にアクセスする方法はありますか?このよう

const a = (arg0, arg1, arg2) => { 
    console.log(arg0); 
}; 
const b = a.bind(null, 1, 2, 3) 

は今、私は唯一のBがあると、それはの事前バインド値のリストを取得することは可能でしょうか?私は値1、2、3を意味する

どうすればいいですか?

+0

あなたがイエスよりも、いくつかの変数内のすべてのこれらのparamsを、持っていた場合、コードを実行して確認してください。 'var b = 5 + 2'と* get * 2と同じように... – Justinas

+0

@Justinasは正確にはありませんが、私はbを持っていますので、b.call()を実行すると引数は確実です。それは確かなので、私は何らかの方法でそれを知ることができますか? –

+0

それで、すべてのパラメータを関数内部に渡したいのですか? – Justinas

答えて

1

独自のバインド関数を記述すると、新しいプロパティをアタッチすることができます。関数には、ほかのオブジェクトと同様にプロパティが追加できます。

function bind(fn, thisArg, ...boundArgs) { 
 
    const func = function(...args) { 
 
    return fn.call(thisArg, ...boundArgs, ...args) 
 
    } 
 
    // you can hide the properties from public view using 
 
    // defineProperties, unfortunately they are still public 
 
    Object.defineProperties(func, { 
 
    __boundArgs: { value: boundArgs }, 
 
    __thisArg: { value: thisArg }, 
 
    __boundFunction: { value: fn } 
 
    }) 
 
    return func 
 
} 
 

 
const a = (a, b, c) => console.log(a, b, c) 
 

 
const b = bind(a, null, 1, 2, 3) 
 

 
b() 
 

 
console.log(b.__boundArgs) 
 
console.log(b.__thisArgs) 
 
console.log(b.__boundFunction)
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

Function.prototype.bindの最初の引数はthis引数です。

クロムを使用している場合は、バインドされた機能の[[BoundArgs]]プロパティの情報にアクセスできます。他にはできませんので、お使いのコンソール

const a = (arg0, arg1, arg2) => { 
 
    console.log(arg0, arg1, arg2); 
 
}; 
 
const b = a.bind(null, 1, 2, 3) 
 
b() 
 
console.dir(b)

+0

私はBoundArgsがそこにあるのを見ることができますが、どうすれば入手できますか? –

+0

私はNode.js envに入っています.BoundArgsがconsole.dir()でコンソールにあるのがわかりますが、どうすれば入手できますか? t.bound() VM501:1 Uncaught TypeError:t.boundは関数ではありません。(...) –

+0

あなたは自分のバインド関数を書くことができるとは思いません。 – synthet1c

関連する問題