ecmascript-5

Automatic semicolon insertion & return statements [duplicate]

此生再无相见时 提交于 2019-11-26 03:44:23
问题 This question already has answers here : What are the rules for JavaScript's automatic semicolon insertion (ASI)? (5 answers) Closed 4 years ago . As you might know, ECMAscript tries to be smart and will automatically insert semicolons if you didn\'t write those explicitly. Simple example function foo() { var bar = 5 return bar } will still work as expected. But there are some caveats if you rely on that. If we re-write that function like so function foo() { var bar = 5 return { bar: bar } }

how to stop Javascript forEach? [duplicate]

强颜欢笑 提交于 2019-11-26 02:34:30
This question already has an answer here: Short circuit Array.forEach like calling break 28 answers i'm playing with nodejs and mongoose — trying to find specific comment in deep comments nesting with recursive func and foreach within. Is there a way to stop nodejs forEach? As i understand every forEach iteration is a function and and i can't just do "break",only "return" but this won't stop foreach. function recurs(comment){ comment.comments.forEach(function(elem){ recurs(elem); //if(...) break; }); } slezica You can't break from a forEach . I can think of three ways to fake it, though. 1.

Creating range in JavaScript - strange syntax

家住魔仙堡 提交于 2019-11-26 02:16:01
问题 I\'ve run into the following code in the es-discuss mailing list: Array.apply(null, { length: 5 }).map(Number.call, Number); This produces [0, 1, 2, 3, 4] Why is this the result of the code? What\'s happening here? 回答1: Understanding this "hack" requires understanding several things: Why we don't just do Array(5).map(...) How Function.prototype.apply handles arguments How Array handles multiple arguments How the Number function handles arguments What Function.prototype.call does They're

Why is it Object.defineProperty() rather than this.defineProperty() (for objects)?

北城以北 提交于 2019-11-26 01:50:00
问题 I\'m working on a JavaScript project, and was just wondering why an object instance doesn\'t inherit the defineProperty() and other methods, rather than having to call the superclass (superobject?) Object method. I\'ve looked at the MDN docs, and there are in fact \"non-standard\" property methods. But those are deprecated. Why would the move be to the Object methods? It seems to me that something like instance.defineProperty(...) is better than Object.defineProperty(instance, ...) . I would

how to stop Javascript forEach? [duplicate]

大城市里の小女人 提交于 2019-11-26 01:48:15
问题 This question already has answers here : Short circuit Array.forEach like calling break (28 answers) Closed 2 years ago . i\'m playing with nodejs and mongoose — trying to find specific comment in deep comments nesting with recursive func and foreach within. Is there a way to stop nodejs forEach? As i understand every forEach iteration is a function and and i can\'t just do \"break\",only \"return\" but this won\'t stop foreach. function recurs(comment){ comment.comments.forEach(function(elem

Get array of object's keys

亡梦爱人 提交于 2019-11-25 23:36:46
问题 I would like to get the keys of a JavaScript object as an array, either in jQuery or pure JavaScript. Is there a less verbose way than this? var foo = { \'alpha\' : \'puffin\', \'beta\' : \'beagle\' }; var keys = []; for (var key in foo) { keys.push(key); } 回答1: Use Object.keys: var foo = { 'alpha': 'puffin', 'beta': 'beagle' }; var keys = Object.keys(foo); console.log(keys) // ['alpha', 'beta'] // (or maybe some other order, keys are unordered). This is an ES5 feature. This means it works in

What is the purpose of the var keyword and when should I use it (or omit it)?

醉酒当歌 提交于 2019-11-25 22:12:52
问题 NOTE : This question was asked from the viewpoint of ECMAScript version 3 or 5. The answers might become outdated with the introduction of new features in the release of ECMAScript 6. What exactly is the function of the var keyword in JavaScript, and what is the difference between var someNumber = 2; var someFunction = function() { doSomething; } var someObject = { } var someObject.someProperty = 5; and someNumber = 2; someFunction = function() { doSomething; } someObject = { } someObject