I am learning and trying to understand JavaScript Objects deeply. I learnt Object prototype
via example below.
var MyObj = function(){
this.Name = "Stackoverflow";
}
MyObj.prototype.prop = "OK";
var instance = new MyObj();
instance.prop = "OK";
console.log(instance.hasOwnProperty("Name")); //true
console.log(Object.getOwnPropertyNames(instance)); //[ 'Name', 'prop' ]
In the example above, instance
object can not access getOwnPropertyNames
of Object
. Because getOwnPropertyNames
function is not a member of prototype of Object
. Following this, when i write my own object like Object
above e.g.
var MyDream = function(){}
MyDream.prototype.canAccessThisMethod = function(x){};
var instanceSample = new MyDream();
instanceSample.canAccessThisMethod("blabla"); //because of prototype
MyDream.method(blabla); // didn't work.
How to make MyDream.method
work in this example?