Javascript: A way to identify location of properties

0 Shares
0
0
0

This is helpful to check and avoid to override properties in an prototype chain.

Lets create a way to find the owner of a specific property given as an argument the name of the property:

Object.prototype.ownerOfProperty = function(propertyName) {

var currentObject = this;

while(currentObject != nulll){

     if (currentObject.hasOwnProperty(propertyName)) {

        return currentObject;

    }

    else {

        currentObject = currentObject.__proto__;

   }

}

return “No property was found”;

};

So we use this by calling it in an object:

ex.  myObject.ownerOfProperty(“toString”); and if it find it then it will access to the myObject prototype as the owner and return the details of the property

0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
“Welcome to San Francisco where a big step of my learning of the technical magic happens"  "Bienvenidos a…
Read More
RESTful routes Here is a good  cheatsheet to take for anyone who is a beginner and its trying…
Read More
I found this quote from Sheryl Sandberg and it remind me to my grandpa.  When I was a…
Read More