ecmascript-5

Weird output of [97,98].map(String.fromCharCode)

邮差的信 提交于 2019-12-01 20:43:59
问题 this works as expected [97,98].map(function(x){String.fromCharCode(x)}) // [ 'a', 'b' ] but the output is following line is unexpected [97,98].map(String.fromCharCode) // [ 'a\u0000\u0000', 'b\u0001\u0000' ] 回答1: String.fromCharCode can accept a variable length of arguments, and treats each one as a character code to build a string arguments.length characters long. map passes several arguments to the inner function. The first, obviously, is the value of the current item. The second is the

splat over JavaScript object (with new)?

烂漫一生 提交于 2019-12-01 20:42:26
问题 How do I splat across objects without using ECMA6 features? Attempt function can(arg0, arg1) { return arg0 + arg1; } function foo(bar, haz) { this.bar = bar; this.haz = haz; } myArgs = [1,2]; With can I can just do: can.apply(this, myArgs); When trying with foo : new foo.apply(this, myArgs); I get this error (because I'm calling new ): TypeError: function apply() { [native code] } is not a constructor 回答1: Using Object.create function foo(bar, haz) { this.bar = bar; this.haz = haz; } x =

splat over JavaScript object (with new)?

亡梦爱人 提交于 2019-12-01 19:50:46
How do I splat across objects without using ECMA6 features ? Attempt function can(arg0, arg1) { return arg0 + arg1; } function foo(bar, haz) { this.bar = bar; this.haz = haz; } myArgs = [1,2]; With can I can just do: can.apply(this, myArgs); When trying with foo : new foo.apply(this, myArgs); I get this error (because I'm calling new ): TypeError: function apply() { [native code] } is not a constructor Using Object.create function foo(bar, haz) { this.bar = bar; this.haz = haz; } x = Object.create(foo.prototype); myArgs = [5,6]; foo.apply(x, myArgs); console.log(x.bar); Ven Using Object.create

Weird output of [97,98].map(String.fromCharCode)

試著忘記壹切 提交于 2019-12-01 19:37:57
this works as expected [97,98].map(function(x){String.fromCharCode(x)}) // [ 'a', 'b' ] but the output is following line is unexpected [97,98].map(String.fromCharCode) // [ 'a\u0000\u0000', 'b\u0001\u0000' ] String.fromCharCode can accept a variable length of arguments, and treats each one as a character code to build a string arguments.length characters long. map passes several arguments to the inner function. The first, obviously, is the value of the current item. The second is the index in the array, which is where the \u0000 and \u0001 come from (add more character codes and you get \u0002

How to use a computed property name in ES5?

耗尽温柔 提交于 2019-12-01 17:05:13
I would like to have a computed property name. I saw you can have this in ES6. But it should be compatible with IOS Webview. So I can't use ES6. Also the computed name will be ever the same inside the loop, if this makes it easier for somebody. Any Ideas? var today = moment().format('DD.MM.YY'); for (var i = 0; i < 5; i++) { initialData.push( { dates: { "01.01.01": false // instead of 01.01.01 i would like to have the value of today as the key } } ) } You have to do it the elaborate way in ES5: var today = moment().format('DD.MM.YY'); var obj = {}; obj[today] = false; for (var i = 0; i < 5; i+

How to use a computed property name in ES5?

假如想象 提交于 2019-12-01 17:04:16
问题 I would like to have a computed property name. I saw you can have this in ES6. But it should be compatible with IOS Webview. So I can't use ES6. Also the computed name will be ever the same inside the loop, if this makes it easier for somebody. Any Ideas? var today = moment().format('DD.MM.YY'); for (var i = 0; i < 5; i++) { initialData.push( { dates: { "01.01.01": false // instead of 01.01.01 i would like to have the value of today as the key } } ) } 回答1: You have to do it the elaborate way

javascript defineProperty to make an attribute non enumerable

荒凉一梦 提交于 2019-12-01 16:58:39
I'm trying to use defineProperty to made attributes not appear in for...in cycle, but it doesn't work. Is this code correct? function Item() { this.enumerable = "enum"; this.nonEnum = "noEnum"; } Object.defineProperty(Item, "nonEnum", { enumerable: false }); var test = new Item(); for (var tmp in test){ console.log(tmp); } Item does not have a property named nonEnum ( check it out ). It is a (constructor) function that will create an object that has a property called nonEnum . So this one would work: var test = new Item(); Object.defineProperty(test, "nonEnum", { enumerable: false }); You

javascript defineProperty to make an attribute non enumerable

夙愿已清 提交于 2019-12-01 15:57:46
问题 I'm trying to use defineProperty to made attributes not appear in for...in cycle, but it doesn't work. Is this code correct? function Item() { this.enumerable = "enum"; this.nonEnum = "noEnum"; } Object.defineProperty(Item, "nonEnum", { enumerable: false }); var test = new Item(); for (var tmp in test){ console.log(tmp); } 回答1: Item does not have a property named nonEnum (check it out). It is a (constructor) function that will create an object that has a property called nonEnum . So this one

using bitwise OR in javascript to convert to integer

被刻印的时光 ゝ 提交于 2019-12-01 12:28:14
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 ? 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 32 bit integer minus 1, which is 2147483647 . (Math.pow(2, 32) / 2 - 1)|0; // 2147483647 (Math.pow(2, 32) /

How can I create an object of fixed structure?

落爺英雄遲暮 提交于 2019-12-01 11:14:26
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) { buffer.push(listItems); }, rotate: function() { buffer.rotateLeft(); } } })(); With imageListItem ,