4 ms·
Another way to call a function without parentheses is with an ES5 getter (or ES6 proxy): // The function we want to call function foo () { console.log(
by toomim 8y ago
Another way to call a function without parentheses is with an ES5 getter (or ES6 proxy):
// The function we want to call
function foo () { console.log('Hello!') }
// ES5 getter:
x = {}
Object.defineProperty(x, 'foo', {get: foo})
// Call it:
x.foo // Outputs 'Hello!'
What if we want to say "foo" instead of "x.foo"? Put a getter on window:
Object.defineProperty(window, 'bar', {get: foo})
// Call it:
bar // Outputs "Hello!"
To pass arguments, use a setter instead:
Object.defineProperty(window, 'hello', {set:
function (x) {console.log('Hello '+x+'!')}
})
// Call it:
hello = 'michael' // Outputs "Hello michael!'
To support multiple arguments, pass an array or hash.
- Me1000 8y agoYou can also invoke the function by doing foo["bar"]: https://runkit.com/me1000/5ba283ad8bb2580012c875e8 https://runkit.com/me1000/5ba283ad8bb2580012c875e8
- SwellJoe 8y ago// Call it: hello = 'michael' // Outputs "Hello michael!' This makes me uncomfortable.
- AzMoo_ 8y agoWith great power comes great responsibility. This is a situation in which the old axiom, "don't write stupid code", comes into play.
- shawn 8y agoBut stupid code is the most fun.
- exikyut 8y agoThis should ideally trigger XHR back to the server (preferably async, with no error checking or retry) which then invokes a SELECT for nonexistant rows on a schema with a bunch of query triggers attached that store the queried-for data after first performing an HTTP request (from inside the DB, in a stored procedure) to translate the data to another language. (The translation service does not provide an API; the stored procedure extracts the response out of the HTML using a combination of 13 regular expressions and 2 substr() calls.) The original un-translated data should not be stored.
- hajile 8y agoYou can also call a constructor passing no arguments without the parens. `var x = new Foo`
- jayflux 8y agoAlthough this is possible, most linters don’t allow it (with default settings on)
- joshribakoff 8y agoThis is precisely why Kyle Simpson says there's no such thing as purity in JS