ecmascript-5

Inconsistent scope of “use strict” on different web browsers (concerning arguments.callee and caller)

亡梦爱人 提交于 2019-11-29 22:18:39
Situation: I found something strange concerning strict mode in Javascript. I am using an external, third-party Javascript library which was minified, has over 4000 lines of code, is not using use strict at all, and is using arguments.callee . I am using use strict in my own code, scoped within a function. When I call one of the functions provided by the library, it throws an error. However, the error is thrown only if I am using use strict the error is thrown in all browsers except Chrome Code: I've removed all the unrelated stuff and reduced the code into this ( online demo on jsFiddle ): //

when do you use Object.defineProperty()

≡放荡痞女 提交于 2019-11-29 22:03:05
I'm wondering when I should use Object.defineProperty to create new properties for an object. I'm aware that I'm able to set things like enumerable: false but when do you need this really? If you just set a property like myObject.myprop = 5; its descriptors are all set to true, right? I'm actually more curious when you guys use that rather verbose call to .defineProperty() and for what reasons. Object.defineProperty is mainly used to set properties with specific property descriptors (e.g. read-only (constants), enumerability (to not show a property in a for (.. in ..) loop, getters, setters).

`new Object` vs `Object` in the ECMAScript spec

与世无争的帅哥 提交于 2019-11-29 19:57:49
So, I'm looking at the ES5 specification at the definition of what new Object and Object do. To my surprise: new Object describes a whole algorithm of how the object constructor works - treating what happens with different kinds of values. Basically calls ToObject on non objects - identity on objects and builds on null and undefined. Object has a special first step for null and undefined where it builds an object and then calls ToObject on primitives and identity on objects. After reading the description a few times - they seem identical. However, clearly from the spec they do something

Is there any way to check if strict mode is enforced?

元气小坏坏 提交于 2019-11-29 19:56:26
Is there anyway to check if strict mode 'use strict' is enforced , and we want to execute different code for strict mode and other code for non-strict mode. Looking for function like isStrictMode();//boolean The fact that this inside a function called in the global context will not point to the global object can be used to detect strict mode: var isStrict = (function() { return !this; })(); Demo: > echo '"use strict"; var isStrict = (function() { return !this; })(); console.log(isStrict);' | node true > echo 'var isStrict = (function() { return !this; })(); console.log(isStrict);' | node false

(Open Source) Examples of JavaScript Prototypical OO

白昼怎懂夜的黑 提交于 2019-11-29 19:27:21
Bounty Edit: I'm looking for code written in a pure prototypical OO paradigm (think Self). Not a mixture of prototypical OO and classical OO. I don't want to see generic OO wrappers but simply usage of prototypical OO techniques and only prototypical OO techniques. Reference Related Question: Prototypical OO in JavaScript In the above question I mainly focused on Can write prototypical OO like this? Do we need constructors and initialization logic, What are the alternatives? New question: Basically are there any good examples of javascript prototypical OO in large open source projects?

What is the difference between using Object.create() and using assignment operator?

自闭症网瘾萝莉.ら 提交于 2019-11-29 17:27:42
Here are a few examples. // case 1: var obj1 = {msg : 'Hello'}; var obj2 = obj1; obj2.msg = "Hi!"; //overwrites alert(obj1.msg); //=>'Hi!' // case 2: var obj1 = {msg : 'Hello'}; var obj2 = Object.create(obj1); obj2.msg = "Hi!"; //does not overwrite alert(obj1.msg); //=>'Hello' // case 3: var obj1 = {data: { msg : 'Hello'}} var obj2 = Object.create(obj1); obj2.data.msg = "Hi!"; //overwrites, Why? alert(obj1.data.msg); //=>'Hi!' I think Object.create() just gives both makes both point to the same prototype, while assignment makes both object point to same location(not just prototype). But then

How to understand JS realms

痞子三分冷 提交于 2019-11-29 17:18:55
问题 In ECMAScript specification there is notion of "realms" introduced: Before it is evaluated, all ECMAScript code must be associated with a realm. Conceptually, a realm consists of a set of intrinsic objects, an ECMAScript global environment, all of the ECMAScript code that is loaded within the scope of that global environment, and other associated state and resources. In Rauschmayer's book "Speaking JavaScript" author writes about objects which can cross realms: In web browsers, each frame and

Equivalent of set in ES6 to ES5

旧城冷巷雨未停 提交于 2019-11-29 16:09:06
I have a set over which I am iterating in ES6. I am trying to convert it to its equivalent in ES5. My build is getting failed because of ES6. That's why I am converting it to ES5. Here's my code in ES6 service.getDevices = function (date) { var result = []; var deviceList = devices[date.getTime()]; for (let item of deviceList) { // browser compatibility: support for ECMA6 result.push({"deviceName": item}); } return result; } I am getting error because of 'let'. I tried using for (var item in deviceList) , it does not display the charts. I also tried this: for(var i = 0; i < deviceList.length()

Browser support for array.includes and alternatives

放肆的年华 提交于 2019-11-29 11:47:29
问题 I looked it up and found this regarding finding a substring in a larger string in an array. Array.Prototype.includes if (t.title.includes(searchString)) My t is part of a $.each that's iterating through a larger array of objects (each objects got a buttload of info, from strings, dates and such). searchString is whatever the user typed in a box. All this is a simple search function for a list I have on the page. This works just fine in Chrome. But Firefox and IE are turning up errors stating

Why does Array.prototype.reduce not have a thisObject parameter?

最后都变了- 提交于 2019-11-29 11:31:48
问题 Javascript Array methods such as forEach have a thisArg parameter, which is used as the context for invoking the callback: array.forEach(callback[, thisArg]) as do every , some , filter and map . However, reduce and reduceRight have no such parameter. Is there some particular reason for this, or some reason it is not necessary? For instance, consider the following implementation of functional composition using reduceRight : function compose () { var fns = [].slice.call(arguments,0); return