defineproperty

TypeScript: How to add static methods to built-in classes

孤者浪人 提交于 2021-01-21 04:18:29
问题 Is there anyhow anyway to add some static method to types like Date , String , Array , etc? For example I want to add method today to Date class and in JavaScript I can simply add a property to it or maybe I use Object.defineProperty: Date.today = function(){ let date = new Date; date.setHours(0,0,0,0); return date; } Object.defineProperty(Date, 'today', { get() { ... }}); But I didn't find anyway to inform TypeScript about this new static member. Am I missing something or Did I google it the

Using Object.DefineProperty and accessing a variable in private scope

▼魔方 西西 提交于 2020-01-24 06:23:13
问题 The following doesn't work, from my getter, I can't see _nickname defined in the 'class' Person. var Person = function (args) { var _nickname = ''; if (args === undefined || args === null) { return; } if (args.nickname !== undefined && args.nickname !== null) { _nickname = args.nickname; } } Object.defineProperty(Person.prototype, "nickname", { get : function () { return _nickname; } }); var x = new Person({ nickname : 'bob' }); console.log(x.nickname); How should one go about accomplishing

Using Object.DefineProperty and accessing a variable in private scope

和自甴很熟 提交于 2020-01-24 06:23:05
问题 The following doesn't work, from my getter, I can't see _nickname defined in the 'class' Person. var Person = function (args) { var _nickname = ''; if (args === undefined || args === null) { return; } if (args.nickname !== undefined && args.nickname !== null) { _nickname = args.nickname; } } Object.defineProperty(Person.prototype, "nickname", { get : function () { return _nickname; } }); var x = new Person({ nickname : 'bob' }); console.log(x.nickname); How should one go about accomplishing

Object.defineProperty or .prototype?

≯℡__Kan透↙ 提交于 2020-01-21 03:04: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

Trigger action on programmatic change to an input value

两盒软妹~` 提交于 2020-01-09 04:39:25
问题 My objective is to observe an input value and trigger a handler when its value gets changed programmatically . I only need it for modern browsers. I have tried many combinations using defineProperty and this is my latest iteration: var myInput=document.getElementById("myInput"); Object.defineProperty(myInput,"value",{ get:function(){ return this.getAttribute("value"); }, set:function(val){ console.log("set"); // handle value change here this.setAttribute("value",val); } }); myInput.value="new

How to remove the setter from a JavaScript object?

你说的曾经没有我的故事 提交于 2020-01-04 03:32:26
问题 Consider the following code: var x = 0; var o = {}; function getter() { return x; } Object.defineProperty(o, "y", { get: getter, set: function (y) { x = y; Object.defineProperty(o, "y", { get: getter }); }, configurable: true }); My objective is to remove the setter and make the property o.y non-configurable after the setter has been called once. However it doesn't work as expected: > x // 0 > o.y // 0 > o.y = 1 // 1 > x // 1 > o.y // 1 > o.y = 2 // 2 > x // 2 > o.y // 2 So my code did not

Make a property either invokable or not

删除回忆录丶 提交于 2020-01-04 02:43:23
问题 I want to make a property either invokable or not. So for example: function Test () { var obj = { someString: 'here is text' }; Object.defineProperty(obj, 'string', { get: function() { return obj.someString; }, set: function() { return function(val) { obj.someString = val; } } }); return obj; } var test = new Test(); That way I could do: test.string // initially returns 'here is text' test.string('new text here') // sets obj.someString to 'new text here' test.string // returns 'next text here

Object define property for getter and setter

橙三吉。 提交于 2020-01-02 13:56:42
问题 I'm getting an error on Maximum call stack size for this code. function ValueObject() { } ValueObject.prototype.authentication; Object.defineProperty(ValueObject.prototype, "authentication", { get : function () { return this.authentication; }, set : function (val) { this.authentication = val; } }); var vo = new ValueObject({last: "ac"}); vo.authentication = {a: "b"}; console.log(vo); Error RangeError: Maximum call stack size exceeded 回答1: That's because the set function is executed each time

Assigning object properties (in a loop) and including a setter results in undefined values

流过昼夜 提交于 2019-12-25 01:48:44
问题 This is my first attempt at using Javascript's Object.defineProperty and/or defineProperties and apparently I'm doing it completely incorrectly. I am passing a cfg object into a Javascript function constructor, then inside that constructor am looping over the config and Calling Object.defineProperty on each cfg key Assigning this[key] = cfg[key] However every this[key] emerges with undefined as it's value. I've tried a few different things, such as setting the writable attribute to true, but