ecmascript-5

Sort an array according to a property which may be null

喜欢而已 提交于 2020-06-08 19:58:31
问题 I ve an array of objects : let items = [ { name: 'eric', value: 1 }, { name: 'bob', value: 4 }, { name: 'michael', value: 0 }, { name: 'john', value: 3 }, { name: 'brad', value: null }, { name: 'martin', value: 2 }, { name: 'chris', value: null } ]; i want to sort my array so that the objects can be sorted by the "value" attribute , and if it's null , make the object in the bottom of the array : { name: 'michael', value: 0 }, { name: 'eric', value: 1 }, { name: 'martin', value: 2 }, { name:

Access the value of Symbol(id) property on an object

落花浮王杯 提交于 2020-05-24 21:38:07
问题 I have an object fetched from 3rd party API as shown below: { name:"Luke Skywalker", __typename:"People", Symbol(id):"ROOT_QUERY.people." } While "Luke Skywalker" can be accessed by simply object.name , how can I get access to the value of Symbol(id) property of this object? 回答1: That object initializer is invalid, so it's hard to answer. If that really is a Symbol-named property, the answer depends on whether the Symbol is globally-registered. If it isn't, you can only discover the symbol

Any performance benefit to “locking down” JavaScript objects?

假装没事ソ 提交于 2020-04-07 13:46:55
问题 JavaScript 1.8.5 (ECMAScript 5) adds some interesting methods that prevent future modifications of a passed object, with varying degrees of thoroughness: Object.preventExtensions(obj) Object.seal(obj) Object.freeze(obj) Presumably the main point of these is to catch mistakes: if you know that you don't want to modify an object after a certain point, you can lock it down so that an error will be thrown if you inadvertently try to modify it later. (Providing you've done "use strict"; that is.)

Clubbing both complex objects does not work: Javascript

喜夏-厌秋 提交于 2020-03-04 21:45:14
问题 I have an source object that has the following structure var obj1 = { type: "type1", nested: { level1: [ { field: "field1", value: "val1"}, { field: "field2", value: "val2"}, { level2: [ { field: "abc", value: "11", }, { field: "abc", value: "12", } ] } ] }, in: 0, out: 20 }; Also there is an input object, based on which merging should happen var obj2 = { type: "type1", nested: { level1: [ { field: "field1", value: "val1"}, { field: "field3", value: "val5" } ] }, in: 0, out: 20 }; Based on

Clubbing both complex objects does not work: Javascript

情到浓时终转凉″ 提交于 2020-03-04 21:44:03
问题 I have an source object that has the following structure var obj1 = { type: "type1", nested: { level1: [ { field: "field1", value: "val1"}, { field: "field2", value: "val2"}, { level2: [ { field: "abc", value: "11", }, { field: "abc", value: "12", } ] } ] }, in: 0, out: 20 }; Also there is an input object, based on which merging should happen var obj2 = { type: "type1", nested: { level1: [ { field: "field1", value: "val1"}, { field: "field3", value: "val5" } ] }, in: 0, out: 20 }; Based on

Clubbing both complex objects does not work: Javascript

為{幸葍}努か 提交于 2020-03-04 21:39:56
问题 I have an source object that has the following structure var obj1 = { type: "type1", nested: { level1: [ { field: "field1", value: "val1"}, { field: "field2", value: "val2"}, { level2: [ { field: "abc", value: "11", }, { field: "abc", value: "12", } ] } ] }, in: 0, out: 20 }; Also there is an input object, based on which merging should happen var obj2 = { type: "type1", nested: { level1: [ { field: "field1", value: "val1"}, { field: "field3", value: "val5" } ] }, in: 0, out: 20 }; Based on

Why does Array.prototype.filter() throw an error in Magnolia JavaScript models?

若如初见. 提交于 2020-02-26 00:52:27
问题 I'm attempting to filter a FreeMarker list in a Magnolia JavaScript model using Array.prototype.filter(). List [#assign list = [1, 2, 3]] Model var Model = function() { this.filterList = function(list) { return list.filter(function(item) { return item === 2 }); } }; new Model(); Usage ${model.filterList(list)} However, I get the following error. Caused by: jdk.nashorn.internal.runtime.ECMAException: TypeError: list.filter is not a function Nashorn was implemented using ECMAScript-262 5.1. The

Differentiate a block from an object initializer

倾然丶 夕夏残阳落幕 提交于 2020-02-19 12:35:15
问题 This is more a theoretical question than a practical one. It's about the parsing of some code delimited by curly braces. Here are two examples of object initializers : f({}); ({a:3}) Here are two examples of blocks : ;{} {a:3;} In practice, it seems that {...} makes a block apart if the precedent code requires an expression. But I've never seen such a rule explicit or made obvious in the ECMAScript specification and I'm not even sure it's true. Is there a definitive non ambiguous reference

Differentiate a block from an object initializer

北城余情 提交于 2020-02-19 12:35:14
问题 This is more a theoretical question than a practical one. It's about the parsing of some code delimited by curly braces. Here are two examples of object initializers : f({}); ({a:3}) Here are two examples of blocks : ;{} {a:3;} In practice, it seems that {...} makes a block apart if the precedent code requires an expression. But I've never seen such a rule explicit or made obvious in the ECMAScript specification and I'm not even sure it's true. Is there a definitive non ambiguous reference

CoffeeScript: Getter/Setter in Object Initializers

心不动则不痛 提交于 2020-01-28 13:04:37
问题 ECMAScript allows us to define getters or setters as following: [text/javascript] var object = { property: 7, get getable() { return this.property + 1; }, set setable(x) { this.property = x / 2; } }; I can work around if I'm using a class : [text/coffeescript] "use strict" Function::trigger = (prop, getter, setter) -> Object.defineProperty @::, get: getter set: setter class Class property: '' @trigger 'getable', -> 'x' member: 0 But what if I want to define trigger on the object directly -