hashrocket - javascript object in rocket syntax - Stack Overflow

I want to get a value from this hash in JavaScript:hash={:user_authenticated=>false, :user_email

I want to get a value from this hash in JavaScript:

hash=    {:user_authenticated=>false, :user_email=>"nope"}

hash.user_authenticated

hash[user_authenticated]

hash["user_authenticated"]

hash[:user_authenticated]

Nothing seems to work. I get this error:

SyntaxError: invalid property id

I want to get a value from this hash in JavaScript:

hash=    {:user_authenticated=>false, :user_email=>"nope"}

hash.user_authenticated

hash[user_authenticated]

hash["user_authenticated"]

hash[:user_authenticated]

Nothing seems to work. I get this error:

SyntaxError: invalid property id

Share Improve this question edited Jul 24, 2014 at 23:29 Fabian Lauer 9,9974 gold badges27 silver badges36 bronze badges asked Jul 24, 2014 at 23:17 rikkitikkitumborikkitikkitumbo 9763 gold badges19 silver badges38 bronze badges 2
  • 2 That looks like Ruby, not JavaScript? – user2864740 Commented Jul 24, 2014 at 23:19
  • Javascript object is like {'key1':'value1', 'key2':'value2', ...} – bestalign Commented Jul 24, 2014 at 23:20
Add a ment  | 

1 Answer 1

Reset to default 8

Javascript objects may not be expressed with the rocket syntax like Ruby's hashes.

However, as ECMAScript 6 is adopted, Javascript implementations have gained the ability to use the same symbol, => for anonymous function definitions, though they are referred to as arrow functions or colloquially as fat arrows rather than hash rockets.

For simple functions, there is no difference between definitions with the arrow syntax and traditional functions.

var foo = function (s) { return s.toString() }

and

function foo(s) { return s.toString() }

are equivalent to:

var foo = (s) => { return s.toString() }

additionally, these are both equivalent to:

var foo = (s) => s.toString()

as well as:

const foo = s => s.toString()

However, when using this, the choice between traditional and arrow functions matters, as traditionally defined functions create a new scope for this, whereas arrow functions do not. Examples from the Mozilla doc on Arrow functions by Mozilla Contributors (licensed under CC-BY-SA 2.5):

function Person() {
  // The Person() constructor defines `this` as an instance of itself.
  this.age = 0;

  setInterval(function growUp() {
    // In non-strict mode, the growUp() function defines `this` 
    // as the global object, which is different from the `this`
    // defined by the Person() constructor.
    this.age++;
  }, 1000);
}

var p = new Person();

Here, p.age will always be 0 because the age that is incremented belongs to a this which only exists within the inner function, not the instance of Person which is p. When the inner function is defined as an arrow function:

function Person() {
  // The Person() constructor defines `this` as an instance of itself.
  this.age = 0;

  setInterval(() => {
    // In non-strict mode, the growUp() function defines `this` 
    // as the global object, which is different from the `this`
    // defined by the Person() constructor.
    this.age++;
  }, 1000);
}

var p = new Person();

p.age will be equal to the number of seconds since p was created because the this in inner function is the instance's this.

For more information, please refer to the Mozilla docs.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140733a4613418.html

相关推荐

  • hashrocket - javascript object in rocket syntax - Stack Overflow

    I want to get a value from this hash in JavaScript:hash={:user_authenticated=>false, :user_email

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信