Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: prototype functions showing up in console

Tags:

javascript

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.

enter image description here

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?

EDIT:

For example here's how a jQuery object looks like, no prototype functions shown in the log

enter image description here

like image 657
php_nub_qq Avatar asked Aug 01 '14 07:08

php_nub_qq


People also ask

How do I see the prototype of a function?

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.

What is prototype in console log?

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.

What is the difference between __ proto __ and prototype?

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.

What does __ proto __ mean in JavaScript?

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.


1 Answers

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.

chrome devtools

like image 191
John Sterling Avatar answered Oct 14 '22 19:10

John Sterling