ecmascript-5

using bitwise OR in javascript to convert to integer

大城市里の小女人 提交于 2019-12-01 10:38:26
问题 we can do the following to convert: var a = "129.13"|0, // becomes 129 var b = 11.12|0; // becomes 11 var c = "112"|0; // becomes 112 This seem to work but not sure if this is a standard JS feature. Does any one have any idea if this is safe to use for converting strings and decimals to integers ? 回答1: Yes, it is standard behavior. Bitwise operators only operate on integers, so they convert whatever number they're give to signed 32 bit integer. This means that the max range is that of signed

How can I create an object of fixed structure?

喜夏-厌秋 提交于 2019-12-01 09:41:54
问题 I have the following code inside my revealing module, but I am uncertain with how to declare/define imageListItem , which is strictly a DTO and doesn't really require any information hiding. Am I correctly defining this object? var imageListItem = function() { var _title; Object.defineProperty(this, "title", { get: function () { return _title; }, set: function (value) { _title = value; } } ); }; var imageList = (function () { var buffer = new CBuffer(); return { populate: function (listItems)

which and how javascript function will be called if we have 2 function declarations with the same name?

余生颓废 提交于 2019-12-01 08:40:29
Take a test: <script> function say() { alert( "ABOVE" ); } say(); function say() { alert( "BELOW" ); } </script> The result is "BELOW" for all test (Chrome, Firefox, IE). How does javascript interpreter work in this case? http://jsfiddle.net/jcv6l/ << run the code. Basically, because of hoisting, which pulls all function declarations to the top of the current scope, the interpreter is basically doing this: function say() { alert( "ABOVE" ); } function say() { alert( "BELOW" ); } say(); That is why it always ends up alerting below KernelPanik In this case the interpreter parses the function

ECMAScript multiple Prologue Directives

£可爱£侵袭症+ 提交于 2019-12-01 07:06:22
问题 Certain ECMAScript environments permit switiching into a special mode by means of a Directive Prologue. ECMAScript 5 has "use strict" and others such as asm have their own like "use asm". The docs on Directive Prologues are written in a language that's a little to obtuse for my comprehension level. What is the correct way to construct a Directive Prologue with multiple Directives? My hunch is its: function(){ "use foo"; "use bar"; } But I'm not sure. 回答1: What is the correct way to construct

which and how javascript function will be called if we have 2 function declarations with the same name?

岁酱吖の 提交于 2019-12-01 06:28:31
问题 Take a test: <script> function say() { alert( "ABOVE" ); } say(); function say() { alert( "BELOW" ); } </script> The result is "BELOW" for all test (Chrome, Firefox, IE). How does javascript interpreter work in this case? http://jsfiddle.net/jcv6l/ << run the code. 回答1: Basically, because of hoisting, which pulls all function declarations to the top of the current scope, the interpreter is basically doing this: function say() { alert( "ABOVE" ); } function say() { alert( "BELOW" ); } say();

Is it possible to determine if an object created with Object.create inherits from Array in JavaScript?

谁说胖子不能爱 提交于 2019-12-01 04:20:57
Identifying which objects are which is complicated in JavaScript, and figuring out which objects are arrays has something of a hacky solution . Fortunately, it manages to work in both of the following cases: Object.prototype.toString.call([]); // [object Array] Object.prototype.toString.call(new Array()); // [object Array] Great, no [object Object] in sight! Sadly, this method still manages to fail with this: var arr = Object.create(Array.prototype); Object.prototype.toString.call(arr); // [object Object] This is frustrating, so say the least. My arr object has all the methods of an array, it

Is there any reason to use Object.create() or new in JavaScript?

牧云@^-^@ 提交于 2019-12-01 03:58:16
I've been using the new keyword in JavaScript so far. I have been reading about Object.create and I wonder if I should use it instead. What I don't quite get is that I often need to run construction code, so I don't see how Object.create is going to work at all since it does not trigger any functions to run. Could anyone tell me, In which case should I use Object.create instead of new ? So far, if you want to create an object, you can only use literals: var obj = {}; or the Object constructor. var obj = Object(); But none of these methods let you specify the prototype of the created object.

EcmaScript-6 backward compatibility

时光总嘲笑我的痴心妄想 提交于 2019-12-01 03:10:25
I am curious to understand/figure-out if the ECMAScript-6 new-changes will work on the old browsers or not. Why I am asking this question is: I remember the introduction of 'use strict'; in ECMAScript-5, it was meant for the compatibility with the old versions. That means the old browsers will keep working fine and they will just ignore it when they encounter the 'use strict'; statement while parsing the new JavaScript code. And the new JS-engines will treat the statement 'use strict'; in some special way as detailed here Strict mode . So, coming to the question I seriously doubt and curious

How to understand JS realms

我是研究僧i 提交于 2019-11-30 18:18:05
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 window has its own realm with separate global variables. That prevents instanceof from working for

ECMAScript 5 Date.parse results for ISO 8601 test cases

天大地大妈咪最大 提交于 2019-11-30 15:44:07
What result is right for the following test cases? //Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 console.log(Date.parse("2012-11-31T23:59:59.000Z"));//1354406399000 NaN NaN 1354406399000 NaN console.log(Date.parse("2012-12-31T23:59:59.000Z"));//1356998399000 1356998399000 1356998399000 1356998399000 1356998399000 console.log(Date.parse("2012-12-31T23:59:60.000Z"));//NaN NaN NaN NaN 1356998400000 console.log(Date.parse("2012-04-04T05:02:02.170Z"));//1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 console.log(Date.parse("2012-04-04T24:00:00.000Z"));//NaN 1333584000000