Object Extension in Javascript

I am trying to extend the functionality of Object in this way:

Object.prototype.get_type = function() { if(this.constructor) { var r = /\W*function\s+([\w\$]+)\(/; var match = r.exec(this.constructor.toString()); return match ? match[1].toLowerCase() : undefined; } else { return typeof this; } } 

This is great, but there is a problem:

 var foo = { 'bar' : 'eggs' }; for(var key in foo) { alert(key); } 

There will be 3 cycle cycles. Is there any way to avoid this?

+4
source share
6 answers

First, I am not completely against the proliferation of native types and ECMA-262 5th ed . solves the problems mentioned in other answers and related articles for us in a good manner. See These slides for a good overview.

You can extend any object and define property descriptors that control the behavior of these properties. A property can be made without an enumerated value when accessing the properties of objects in a for..in loop, this property will not be included.

Here you can define the getType method for the Object.prototype itself and make it non-enumerable:

 Object.defineProperty(Object.prototype, "getType", { enumerable: false, writable: false, configurable: false, value: function() { return typeof this; } }); // only logs "foo" for(var name in { "foo": "bar" }) { console.log(name); } 

The getType function above is mostly useless, as it simply returns a typeof object , which in most cases will be just an object, but it is only available for demonstration.

 [].getType(); {}.getType(); (6).getType(); true.getType(); 
+4
source

You should not extend the prototype of an object for this reason:

http://erik.eae.net/archives/2005/06/06/22.13.54/

Use the static method instead.

If you have no choice, you can use the hasOwnProperty method:

 Object.prototype.foo = function(){ alert('x'); } var x = { y: 'bar' }; for(var prop in x){ if(x.hasOwnProperty(prop)){ console.log(prop); } } 
+3
source

You can use the hasOwnProperty() method to check if the property belongs to the foo object:

 var foo = { 'bar' : 'eggs' }; for (var key in foo) { if (foo.hasOwnProperty(key)) { alert(key); } } 
+3
source

Is there any way to avoid this?

Yes, do not propagate your own types.

Use a wrapper instead:

 var wrapper = (function(){ var wrapper = function(obj) { return new Wrapper(obj); }; function Wrapper(o) { this.obj = obj; } Wrapper.prototype = wrapper.prototype; return wrapper; }()); // Define your get_type method: wrapper.prototype.get_type = function(){ if(this.obj.constructor) { var r = /\W*function\s+([\w\$]+)\(/; var match = r.exec(this.obj.constructor.toString()); return match ? match[1].toLowerCase() : undefined; } else { return typeof this.obj; } }; 

Using:

 var obj = { 'bar' : 'eggs' }; alert(wrapper(obj).get_type()); for(var i in obj) { ... works properly } 
+2
source

Create your own object instead of extending the default object.

See also:

+1
source

When you iterate over enumerated properties of an object, you can determine whether the current property has been "inherited" or not using Object.hasOwnProperty ()

 for ( var key in foo ) { if ( foo.hasOwnProperty( key ) ) { alert(key); } } 

But let the dangers associated with monkey patches be known to you, especially on Object , as others posted about

+1
source

Source: https://habr.com/ru/post/1310974/


All Articles