最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hashrocket - javascript object in rocket syntax - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论