I just noticed that when I log an instance of the object I'm currently working on I see the prototype functions after it's property ( it only has one ). Which makes me think I'm doing something wrong.
This is how I'm setting the prototype.
MF = function(x){
if(!(this instanceof MF)) return new MF(x);
this.node = x;
}
MF.prototype = {
show: function(display){
display ? this.node.style.display = display : this.node.style.display = 'block';
},
hide: function(){
this.node.style.display = 'none';
}
}
console.log(MF(window));
I also tried setting it with Object.create()
, as suggested in this answer, which doesn't make much sense in my case but desperate times call for desperate attempts.
Why am I seeing the prototype methods in the instance, and how can I fix this?
For example here's how a jQuery
object looks like, no prototype functions shown in the log
Note: The property of an object that points to its prototype is not called prototype . Its name is not standard, but in practice all browsers use __proto__ . The standard way to access an object's prototype is the Object. getPrototypeOf() method.
The prototype is an object that is associated with every functions and objects by default in JavaScript, where function's prototype property is accessible and modifiable and object's prototype property (aka attribute) is not visible.
The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.
The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array.
There doesn't appear anything wrong with your code. I'm sure the prototype properties are only listed as a convenience when there are only a few instance properties. Expanding the logged object reveals that all properties are where they are supposed to be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With