defineproperty

Error in accessor property: can't redefine non-configurable property 'status'

我们两清 提交于 2019-12-05 12:28:47
问题 I'm trying to define an object and create an accessor property for it. HTML: <input type='hidden' id='crudMode' value='Create' /> JavaScript: crudMode = { create: "Create", read: "Read", update: "Update", delete: "Delete", current: function () { return $('#crudMode').val(); } } Object.defineProperty(crudMode, 'mode', { get: function(){ return this.current(); }, set: function(value){ $('#crudMode').val(value); } }); But when I use it, it throws the mentioned error in the question title:

How to “override” a defined (get-)property on a prototype?

帅比萌擦擦* 提交于 2019-12-04 19:13:21
问题 I have some code which defines a getter (but no setter, if such is relevant) on a prototype. The value returned is correct in 99.99% of the cases; however, the goal is to set the property to evaluate to a different value for a specific object. foo = {} Object.defineProperty(foo, "bar", { // only returns odd die sides get: function () { return (Math.random() * 6) | 1; } }); x = Object.create(foo); x.bar // => eg. 5 x.bar = 4 // by fair dice roll x.bar // nope => eg. 3 How can the property be

Error in accessor property: can't redefine non-configurable property 'status'

好久不见. 提交于 2019-12-03 23:42:09
I'm trying to define an object and create an accessor property for it. HTML: <input type='hidden' id='crudMode' value='Create' /> JavaScript: crudMode = { create: "Create", read: "Read", update: "Update", delete: "Delete", current: function () { return $('#crudMode').val(); } } Object.defineProperty(crudMode, 'mode', { get: function(){ return this.current(); }, set: function(value){ $('#crudMode').val(value); } }); But when I use it, it throws the mentioned error in the question title: console.log(crudMode.mode); Throws: TypeError: can't redefine non-configurable property 'mode' What's wrong

Object.defineProperty or .prototype?

烈酒焚心 提交于 2019-12-03 12:08:30
I've seen two different techniques of implementing non-native features in javascript, First is: if (!String.prototype.startsWith) { Object.defineProperty(String.prototype, 'startsWith', { enumerable: false, configurable: false, writable: false, value: function(searchString, position) { position = position || 0; return this.lastIndexOf(searchString, position) === position; } }); } and Second is: String.prototype.startsWith = function(searchString, position) { position = position || 0; return this.lastIndexOf(searchString, position) === position; } I know that the second one is used to attach

How do I undo a Object.defineProperty call?

随声附和 提交于 2019-12-03 06:20:48
问题 Fiddle var Assertion = function() { return { "dummy": "data" }; } Object.defineProperty(Object.prototype, 'should', { set: function(){}, get: function(){ return new Assertion(this); } }); // Insert magic here. // This needs to be false console.log(({}).should === undefined); What options do I have in ES5 to undo a defineProperty call ? No silly suggestions like Object.defineProperty = function() { } please. The following Object.defineProperty(Object.prototype, 'should', {}) does not work and

How to customize properties in TypeScript

♀尐吖头ヾ 提交于 2019-12-02 22:05:32
How do I get TypeScript to emit property definitions such as: Object.defineProperties(this, { view: { value: view, enumerable: false, writable: false, configurable: false }, }); I was looking for exactly the same thing when I stumbled upon TypeScript Handbook: Decorators . In "Method Decorators" paragraph they define @enumerable decorator factory, which looks like this (I'm simply copy-pasting from there): function enumerable(value: boolean) { return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { descriptor.enumerable = value; }; } and they use it like this:

Object.defineProperty for all browsers?

若如初见. 提交于 2019-12-02 20:32:29
Asking about Object.defineProperty as demonstrated below: function testComponent(){ var testProperty; Object.defineProperty(this, "testProperty", { get : function() { return testProperty; }, set : function(val) { testProperty = val; } }); } Where it would be used like so: testObject = new testComponent(); testObject.testProperty = "testValue"; Based on what I've seen so far, it looks like there is no cross browser solution, as I've tried using es5-shim with no luck, but I would like to confirm. I also found a reference to this post and my tests still fail in IE 7 & 8, can anyone shed any light

How do I undo a Object.defineProperty call?

依然范特西╮ 提交于 2019-12-02 18:50:28
Fiddle var Assertion = function() { return { "dummy": "data" }; } Object.defineProperty(Object.prototype, 'should', { set: function(){}, get: function(){ return new Assertion(this); } }); // Insert magic here. // This needs to be false console.log(({}).should === undefined); What options do I have in ES5 to undo a defineProperty call ? No silly suggestions like Object.defineProperty = function() { } please. The following Object.defineProperty(Object.prototype, 'should', {}) does not work and Object.defineProperty(Object.prototype, 'should', { value: undefined }) Throws a Uncaught TypeError:

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