ecmascript-5

What Internal Property In ECMAScript is defined for?

China☆狼群 提交于 2019-11-26 20:45:36
问题 What the Internal Property in ECMAScript is defined for ? What does the spec mean by This specification uses various internal properties to define the semantics of object values.These internal properties are not part of the ECMAScript language. They are defined by this specification purely for expository purposes. Does it mean that Internal properties defined by ECMAScript are not available for programming. They are used in the implementation of the javascript engine ? 回答1: Internal

Working around IE8's broken Object.defineProperty implementation

牧云@^-^@ 提交于 2019-11-26 19:43:54
问题 Consider the following code, using ECMAScript5's Object.defineProperty feature: var sayHi = function(){ alert('hi'); }; var defineProperty = (typeof Object.defineProperty == 'function'); if (defineProperty) Object.defineProperty(Array.prototype,'sayHi',{value:sayHi}); else Array.prototype.sayHi = sayHi; var a = []; a.sayHi(); This works for Chrome and Firefox 4 (where defineProperty exists), and it works for Firefox 3.6 (where defineProperty does not exist). IE8, however, only partially

Extending Object.prototype JavaScript

∥☆過路亽.° 提交于 2019-11-26 18:52:57
I am not asking if this is okay: Object.prototype.method = function(){}; This is deemed evil by pretty much everyone, considering it messes up for(var i in obj) . The Real Question Ignoring Incompetent browsers(browsers that don't support Object.defineProperty ) Potential for property collision or overriding Assuming you have some incredibly useful method, is this considered wrong/unethical? Object.defineProperty(Object.prototype, 'methodOnSteriods',{ value: function(){ /* Makes breakfast, solves world peace, takes out trash */ }, writable: true, configurable: true, enumerable: false }); If

How to implement private method in ES6 class with Traceur [duplicate]

旧时模样 提交于 2019-11-26 17:57:27
问题 This question already has an answer here: Private properties in JavaScript ES6 classes 35 answers I use Traceur Compiler to have advantage with ES6 features now. I want to implement this stuff from ES5: function Animal() { var self = this, sayHi; sayHi = function() { self.hi(); }; this.hi = function() {/* ... */} } Currently traceur does not support private and public keywords (from harmony). And ES6 class syntax does not allow to use simple var (or let ) statements in class body. The only

What is the difference between 'let' and 'const' ECMAScript 2015 (ES6)?

孤街醉人 提交于 2019-11-26 17:37:50
I'm wondering what is the difference between let and const in ES6 . Both of them are block scoped, as the example in the following code: const PI = 3.14; console.log(PI); PI = 3; console.log(PI); const PI = 4; console.log(PI); var PI = 5; console.log(PI); In ES5 the output will be: 3.14 3.14 3.14 3.14 But in ES6 it will be: 3.14 3 4 5 I'm wondering why ES6 allows the change of const value, the question is why should we use 'const' now? we can use 'let' instead? Note : jsbin can be used for testing, choose JavaScript to run ES5 code and Traceur to run it with ES6 capabilities. What you're

Why are Octal numeric literals not allowed in strict mode (and what is the workaround?)

感情迁移 提交于 2019-11-26 17:23:14
问题 Why are Octal numeric literals not allowed in JavaScript strict mode? What is the harm? "use strict"; var x = 010; //Uncaught SyntaxError: Octal literals are not allowed in strict mode. <h1>Check browser console for errors</h1> In case a developer needs to use Octals (which can mistakenly change a numbers meaning), is there a workaround? 回答1: The "why" part of the question is not really answerable. As for "how", off the top of my head... "use strict"; var x = parseInt('010', 8); document

React: Which is recommended arrow or normal function?

*爱你&永不变心* 提交于 2019-11-26 16:41:46
I started using arrow functions after I felt doing manual function/object bindings and scope related issues are headache but very rencently I came to know that it’s better to use normal function(ES5) than arrow function(ES6). My understanding about these functions Normal function in React: Bind object/function manually in order to play with state or props inside the function and to avoid scope related issues Bind object/function always in constructor but not directly in render If you do it in constructor then Webpack creates new object/function in bundle.js file only once when your component

gulp babel, exports is not defined

。_饼干妹妹 提交于 2019-11-26 16:04:34
问题 Consider the following example code (and maybe I am doing it wrong?) var FlareCurrency = { }; export {FlareCurrency}; I have the following task: gulp.task("compile:add-new-currency-minified", function(){ return gulp.src('src/add-new-currency/**/*.js') .pipe(babel()) .pipe(concat('Flare-AddNewCurrency.js')) .pipe(uglify({"preserveComments": "all"})) .pipe(gulp.dest('dist/minified/')); }); When I run this I get the following: "use strict";Object.defineProperty(exports,"__esModule",{value:!0})

Dynamically set property of nested object

筅森魡賤 提交于 2019-11-26 15:11:47
I have an object that could be any number of levels deep and could have any existing properties. For example: var obj = { db: { mongodb: { host: 'localhost' } } }; On that I would like to set (or overwrite) properties like so: set('db.mongodb.user', 'root'); // or: set('foo.bar', 'baz'); Where the property string can have any depth, and the value can be any type/thing. Objects and arrays as values don't need to be merged, should the property key already exist. Previous example would produce following object: var obj = { db: { mongodb: { host: 'localhost', user: 'root' } }, foo: { bar: baz } };

Automatic semicolon insertion & return statements [duplicate]

情到浓时终转凉″ 提交于 2019-11-26 13:50:17
This question already has an answer here: What are the rules for JavaScript's automatic semicolon insertion (ASI)? 5 answers 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 } } ..that function would now return undefined because the interpreter would insert that semicolon right after the return