Assume you got an object like var obj = {a:'foo',b:'bar'}
you provide to a function function equal(x) { return x.a === x.b};
Is there a way to provide the object argument as scope to remove the object references within the function like so function equal(x) { return a === b};
?
Side note: Syntax preferably works with ECMAScript 5th edition
Rationale: The idea behind the question is to cover cases where the object argument holds many properties {a:'foo',b:'bar',...,z:'baz'}
, which would require repeating the object name quiet often.
In ES6 we can use the destructuring assignment to get the mapping of the key-values in variables:
var obj = {a:'foo',b:'bar'};
//Using ES6 Destructuring syntax
function equal({a, b}) { return a === b};
console.log(equal(obj));
obj = {a:'bar',b:'bar'};
console.log(equal(obj));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With