ecmascript-5

Why is there no forEach method on Object in ECMAScript 5?

纵然是瞬间 提交于 2019-12-02 17:02:59
ECMAScript 5's array.forEach(callback[, thisArg]) is very convenient to iterate on an array and has many advantage over the syntax with a for: It's more concise. It doesn't create variables that we need only for the purpose of iterating. It's creates a visibility scope for the local variables of the loop. It boosts the performance. Is there a reason why there is no object.forEach to replace for(var key in object) ? Of course, we could use a JavaScript implementation, like _.each or $.each but those are performance killers. Alex Wayne Well, it's pretty easy to rig up yourself. Why further

ECMAScript Regex for a multilined string

旧巷老猫 提交于 2019-12-02 13:17:08
I am writing the loading procedure for my application and it involves reading data from a file and creating an appropriate object with appropriate properties. The file consists of sequential entries (separated by a newline) in the following format: === OBJECT TYPE === <Property 1>: Value1 <Property 2>: Value2 === END OBJECT TYPE === Where the values are often strings which may consist of arbitrary characters, new-lines, etc. I want to create a std::regex which can match this format and allow me to use std::regex_iterator to read each of the objects into the file in turn. However, I am having

javascript riddle: 2 objects that seem identical with respect to constructor, prototype and __proto__ link, behave differently

こ雲淡風輕ζ 提交于 2019-12-02 11:44:46
I am an experienced object oriented programmer but this got me! Why am I able to do new f() but not new a(). I will appreciate any pointers. // first a few facts if (Object instanceof Function) console.log("Object isa Function"); console.log("Function.prototype is " + Function.prototype); /* output Object isa Function Function.prototype is function Empty() {} */ var f = new Function(); console.log("Prototype of f:" + f.prototype); console.log("Constructor of f:" + f.constructor); console.log("Prototype Link of f:" + f.__proto__); if (f instanceof Function) console.log("f isa Function"); /*

argumental reference inconsistency in javascript

匆匆过客 提交于 2019-12-02 09:35:47
I have recently encountered a nasty issue in JS. Let say we pass a map, an array of objects to a function f. var o=[{a:0}]; function f(a){ for(var i in a){ if (a.hasOwnProperty(i)){ a[i]=null; } } return a; }; var outp=f(o); alert(outp[0]+" === "+o[0]+" : "+(outp[0]===o[0])); // here we expect loose equality, and equality in type, //furthermore it should identically equal as well, and we got right! But, we can not pass total responsibility of an object to a function as argument, same like in functional paradigm o=(function(o){return o})() , because any kind of modification to o is not

6To5 Compiler - use __proto__ for inheritance

谁说胖子不能爱 提交于 2019-12-02 03:51:02
问题 As the output of 6to5 I've got the following code: var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); // question is about next row if (superClass) subClass.__proto__

getPathValue() function for deep objects with arrays and with packed JSON

江枫思渺然 提交于 2019-12-02 03:27:11
问题 For background, please refer to this question: Access deep object member of embeded JSON The solutions offered there worked very well with the packed JSON contained in key values. However, they don't handle the situation where JSON has arrays. The original function I referenced in the other question DID handle arrays, but it would not handle the packed JSON. This is the original function: function getPathValue(obj, path) { return new Function('_', 'return _.' + path)(obj); } and this is the

What is the difference when we use array names instead of spread operator?

我怕爱的太早我们不能终老 提交于 2019-12-02 01:19:19
What is the difference if I use: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? numbers : ''; instead of this: var numbers = [1, 2, 3] var mainArray = (numbers.length > 1) ? [...numbers] : ''; Since assigment of data structures points to the same space in memory, if you have two variables referencing the same array, altering one variable will alter the other. That is to say: if x = [1, 2, 3] and y = x then we say x.push(5) y will also have that 5 because they are pointing to the same instance. If you use [...x] you are creating a copy of x. It consumes O(n) memory, a new

In ECMAScript, how are some of native objects also built-in?

▼魔方 西西 提交于 2019-12-01 23:45:43
I suppose a definition of native and built-in objects is required to answer this question. Here's what the ECMAScript spec defines these as: 4.3.6 native object object in an ECMAScript implementation, independent of the host environment, that is present at the start of the execution of an ECMAScript program. NOTE Standard native built-in objects are defined in this specification. Some native objects are built-in ; others may be constructed during the course of execution of an ECMAScript program 4.3.7 built-in object object supplied by an ECMAScript implementation, independent of the host

getPathValue() function for deep objects with arrays and with packed JSON

雨燕双飞 提交于 2019-12-01 23:11:26
For background, please refer to this question: Access deep object member of embeded JSON The solutions offered there worked very well with the packed JSON contained in key values. However, they don't handle the situation where JSON has arrays. The original function I referenced in the other question DID handle arrays, but it would not handle the packed JSON. This is the original function: function getPathValue(obj, path) { return new Function('_', 'return _.' + path)(obj); } and this is the answer from the first question: function getValue(object, path) { return path .split('.') .reduce((o, k)

Compare array objects and show difference

点点圈 提交于 2019-12-01 22:52:43
I have two arrays which I want to compare and check if there is an deleted item in one of these arrays. If there is show me the difference (deleted item) Here is the code below how I would like to achieve this: var completedList = [{id:1},{id:2},{id:3},{id:4},{id:7},{id:8}]; var invalidList = [{id:3},{id:4},{id:5},{id:6}]; // filter the items from the invalid list, out of the complete list var validList = completedList.map((item) => { console.log(item.id) return item.id; //console.log(invalidList.id); }).filter(item => { Object.keys(invalidList).map(key => { console.log(invalidList[key].id) /