ecmascript-5

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

≯℡__Kan透↙ 提交于 2019-11-28 16:16:41
问题 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 回答1: 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)

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

浪尽此生 提交于 2019-11-28 15:45:42
问题 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

Prototypical OO in JavaScript

拟墨画扇 提交于 2019-11-28 15:18:06
TL;DR: Do we need factories/constructors in prototypical OO? Can we make a paradigm switch and drop them completely? The BackStory: I've been toying with doing prototypical OO in JavaScript lately and find that 99% of OO done in JavaScript is forcing classical OO patterns into it. My take on prototypical OO is that it involves two things. A static prototype of methods (and static data) and a data binding. We don't need factories or constructors. In JavaScript these are Object literals containing functions and Object.create . This would mean we can model everything as a static blueprint

How can I make old style classes work in typescript?

旧巷老猫 提交于 2019-11-28 14:04:04
While converting a large number of files to typescript, I have many classes declared this way. function FooClass() { this.bar = 1; // error TS2683: 'this' implicitly has type 'any' // because it does not have a type annotation. } FooClass.prototype.myMethod = function() { // ... } How can I make this work with strict type checking turned on, while avoiding rewriting it all using class syntax? The easiest way to get the above code to work is to add a this parameter to the function, like so: function FooClass(this: {bar: number}) { this.bar = 1; // okay } Unfortunately, you will soon find that

Revisiting extending native prototypes after ECMAScript 5

喜欢而已 提交于 2019-11-28 13:44:18
Recently, given the changes to defining properties in ECMAScript 5, I have revisited the question of whether we can safely extend the native JavaScript prototypes. In truth, all along I have extended prototypes like Array and Function, but I avoided doing so with Object, for the obvious reasons. In unit testing with Jasmine, by adding Object.prototype specs to specs for my own personal framework, extending Object.prototype with non-enumerable functions has appeared to be safe. Data properties like a "type" property, however, with getters/setters that do any unusual processing have had

Is the 'catch' method name of JS Promises/A+ invalid since it's a JS keyword?

北城余情 提交于 2019-11-28 13:30:45
I started to use JS Promises in a project recently. I noticed that every time I use .catch my JS linter complains. It does run and does what it should but I looked up the ECMAScript spec and it really looks like it is right: Since catch is a keyword it can not be used as an identifier. As I understand method names are identifiers, so this is invalid: Promise.reject("Duh").catch(alert); It should be this instead: Promise.reject("Duh")['catch'](alert); What am I missing? Bergi What am I missing? A property name is not an identifier, it can use any identifier name. From the spec on Property

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

这一生的挚爱 提交于 2019-11-28 12:49:07
问题 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

Do reserved words need to be quoted when set as property names of JavaScript objects?

本小妞迷上赌 提交于 2019-11-28 10:50:57
Given an object literal, or jQuery(html, attributes) object, does any specification state that reserved words, or future reserved words MUST be quoted? Or, can, for example, class be set as a property name of an object without using quotes to surround the property name, without the practice being contrary to a specification concerning identifiers, property names, or use of reserved words? Seeking a conclusive answer as to this question to avoid confusion. let objLit = { class: 123, var: "abc", let: 456, const: "def", import: 789 } console.dir(objLit); jQuery("<div>012</div>", { class: "ghi" })

Object.keys() complexity?

偶尔善良 提交于 2019-11-28 10:44:34
Anyone know the time-complexity of ECMAScript5's Object.keys() in common implementations? Is it O(n) for n keys? Is time proportional to the size of the hash table, assuming a hash implementation? I'm looking for either guarantees by language implementors or some real world benchmarking. It appears to be O(n) in V8 (chrome, node.js) at least: > var hash = {} > , c = 0; > > var s = +new Date();Object.keys(hash);console.log(+new Date() - s); 0 > for(var i=0; i<100000; i++, c++){ hash[c] = 1; } > var s = +new Date();Object.keys(hash);console.log(+new Date() - s); 26 > for(var i=0; i<100000; i++,

Node.js Global eval, throwing ReferenceError

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 08:46:11
I am trying to learn JavaScript from the Rhino book. I was trying to execute the following code from the book with regards to eval() . I am using node.js (v0.10.29) to execute the examples. var geval = eval; // aliasing eval to geval var x = 'global'; // two global variables var y = 'global'; function f () { var x = 'local'; // define a local variable eval('x += "changed";'); // direct eval sets the local variable return x; } function g () { var y = 'local'; // define a local variable geval('y += "changed";'); // indirect eval sets global variable return y; } console.log(f(), x); // =>