Adding a prototype to a JavaScript object

STORE = { item : function() { } }; STORE.item.prototype.add = function() { alert('test 123'); }; STORE.item.add(); 

I tried to find out what happened with this for quite some time. Why is this not working? However, it works when I use the following:

 STORE.item.prototype.add(); 
+51
javascript prototype object-literal
Oct. 20 '09 at 4:09
source share
5 answers

The prototype object is intended for use, mainly functions that will be called using the new operator to create new instances of objects.

Functions in JavaScript are first-class objects, which means that you can add elements to them and process them just like regular objects:

 var STORE = { item : function() { } }; STORE.item.add = function() { alert('test 123'); }; STORE.item.add(); 

A typical use of a prototype object, as I said earlier, is when you instantiate an object by calling a constructor function with a new statement, for example:

 function SomeObject() {} // a constructor function SomeObject.prototype.someMethod = function () {}; var obj = new SomeObject(); 

All instances of SomeObject inherit members from SomeObject.prototype , because these elements will be accessed through a prototype chain.

Every function in JavaScript has a prototype object, because there is no way to know which functions are intended to be used as constructors.

+79
20 Oct '09 at 4:15
source share

After many years when JavaScript (ES2015 arrives), we finally Object.setPrototypeOf ()

 const STORE = { item: function() {} }; Object.setPrototypeOf(STORE.item, { add: function() { alert('test 123'); } }) STORE.item.add(); 
+12
Apr 19 '16 at 11:45
source share

You can use JSON revivers to turn JSON into class objects during parsing. The EcmaScript 5 project adopted the JSON2 peer review scheme described at http://JSON.org/js.html

 var myObject = JSON.parse(myJSONtext, reviver); 

An additional parameter to reviver is a function that will be called for each key and the value at each level of the final result. Each value will be replaced by the result of the restore function. This can be used to reform generic objects into instances of pseudo-classes, or convert string dates to Date objects.

 myData = JSON.parse(text, function (key, value) { var type; if (value && typeof value === 'object') { type = value.type; if (typeof type === 'string' && typeof window[type] === 'function') { return new (window[type])(value); } } return value; }); 
+2
Oct. 20 '09 at 5:12
source share

At the time of this writing, this is possible using the __proto__ property. Just in case, someone here is checking now and probably in the future.

 const dog = { name: 'canine', bark: function() { console.log('woof woof!') } } const pug = {} pug.__proto__ = dog; pug.bark(); 

However, the recommended way to add a prototype in this case is Object.create . Therefore, the above code will be translated into:

 const pug = Object.create(dog) pug.bark(); 

Or you can also use Object.setPrototypeOf as indicated in one of the answers.

Hope this helps.

+2
Sep 28 '16 at 10:03
source share
 STORE = { item : function() { } }; 

this command will create a STORE object. You can check by typeof STORE; He must return the "object." And if you are STORE.item; it returns a "function ..".

Since this is a regular object, therefore, if you want to change the function of an element, you can simply access its properties / method with this command.

 STORE.item = function() { alert('test 123'); }; 

Try STORE.item; it still should return a 'function ..'.

Try STORE.item(); then a warning will be shown.

0
Jun 23 '19 at 15:09
source share



All Articles