0

Please help me edit the title.

Basically, what I mean by 'defining arrows' is making "->" have a function.

For example, let say I give "->" a function:

console.log("Hello")

So, when I use this:

->

Javascript will automatically do 'console.log("Hello")'.

I tried this method but failed to work:

function ->{
    console.log("Hello")
}

My question is, is there a method for what I want? Why or why not?

Lok Jun
  • 1,315
  • 2
  • 9
  • 16
  • You cannot have a function name starting with `-` – SajithNair Mar 22 '14 at 05:05
  • @p.s.w.g Please put it as an answer. Basically, I hope this will help someone else when they come here. – Lok Jun Mar 22 '14 at 05:07
  • this is basically a copy of http://stackoverflow.com/questions/20728460/is-it-possible-to-create-custom-operators-in-javascript – Vicky Mar 22 '14 at 05:09
  • @Vicky sorry, basically I dont even know what to search. – Lok Jun Mar 22 '14 at 05:14
  • @leonneo no harm meant bro ..... just pointed you out in the right direction ...... that post will help you out if u check it xD – Vicky Mar 22 '14 at 05:16
  • @leonneo You might want to see this Wikipedia article: [Operator overloading](http://en.wikipedia.org/wiki/Operator_overloading) – p.s.w.g Mar 22 '14 at 05:16

1 Answers1

1

No, you can't do that. -> is not a valid identifier, and you can't define custom operators in JavaScript (if that's what you were going for). From MDN:

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the \uXXXX Unicode escape sequences as characters in identifiers.

Of course, you can use bracket notation to define members on objects that are not valid identifiers, like this:

window['->'] = function() {
    console.log("Hello");
}

But then you would have to call with this rather unwieldy syntax:

window['->']();
Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • So, for the window[] function, can you remove the parenthesis? – Lok Jun Mar 22 '14 at 05:16
  • @leonneo no, `window` is basically an object that represents the global context inside a browser (e.g. `alert` is actually `window.alert`) and the brackets (`[…]`) are used to access a member of that object by specifying its name as a string (which works even if the name isn't a valid identifier). And no, you can't remove the parentheses; `window['->']` is a reference to the function, putting the parentheses at the end is what invokes the function. – p.s.w.g Mar 22 '14 at 05:20
  • Then is there a way to insert 2 functions in one window tag like this: window["-> <-"] – Lok Jun Mar 22 '14 at 05:22
  • @leonneo No. The string will be used to access a property of the window object. `window['-> <-']` will return the property of `window` with the name `'-> <-'` if one exists, or `undefined` if one does not exist. – p.s.w.g Mar 22 '14 at 05:24