JavaScript object output to console.log

I want to know where the .log console gets the name of the build function when printing the object. Also, does this really affect any code?

function F() { this.test = 'ok'; } var f = new F(); console.log( f ); 

Output console.log file (in Chrome): F {test: "ok"}

Where does the .log console get F in F {test... ?

If I change F.constructor , F.prototype and F.constructor to something random, it still prints the original F :

 function G() { this.fail = 'bad'; } function F() { this.test = 'ok'; } F.prototype = G; F.constructor = G; var f = new F(); console.log( f ); 

The output is the same - F {test: "ok"}

Is this information just confidential in the browser, my question is, does it affect JavaScript code? That is, will it creep during comparison or inheritance after I override the properties of the prototype and constructor ?

UPDATE

The original goal was to do the following.

 function Person ( _name ) { this.name = _name; } function Construct( _constructor, _args, _context ) { function F () { var context = _context || this; return _constructor.apply( context, _args ); } /* I want to have the constructed object by identified as _constructor and not a F */ F.prototype = _constructor.prototype; return new F(); } function Make ( _who ) { if ( 'person' === _who ) { /* Remove the first argument, who, and pass along all the rest. Constructors cannot be called with .apply so I have to use this technique. */ return Construct( Person, Array.prototype.slice.call( arguments, 1 ) ); } } var dev = Make( 'person', 'John Doe' ); console.log( dev ); // prints `F {name: "John Doe"}` 

As you can see, the resulting dev response outputs F {name: "John Doe"} , which made me wonder if I could run into problems later if I wanted to do comparisons or inheritance with instances constructed in this way.

+6
source share
5 answers

Changing F.prototype replaces the contents of F , not the name. An old object prototype still exists, and a reference to it is stored inside each instance of old F You buy it by typing f.__proto__ ' (deprecated) or Object.getPrototypeOf(f) .

Note that __proto__ is a proterty accessor (internally a receiver, not a valid property), so it cannot be changed.

+3
source

This is not difficult, since f is, finally, an instance of F and the resolution order of the region (this, prototype, ...) is obvious :-)

For example, you can run this code and you will see that in this case it will print G:

 function G() { this.fail = 'bad'; } function F() { this.test = 'ok'; } F.prototype = G; F.constructor = G; var f = new F(); // Prints F console.log(f); f.prototype = G; // Redefining f type info f.constructor = G; console.log(f); // Prints G 
+1
source

Creating a new instance of F, so the browser prints this to help you keep track of your log. Even if you change the prototype, you still have to create a new โ€œFโ€ to get the object.

 function A () { something: 123 } new A(); console.log result: A {} new B(); console.log result: ReferenceError: B is not defined 
0
source

object.constructor.name is another way to get the name of the constructor of an object.

0
source

Can I suggest a different approach for the initial intention? No rpoblem, just using a different reference to the prototype object instead of the original one, so you can do

 function construct(constructor, args, context) { //lowercase, as it a function, not a class return new constructor(args); } 

This should create the correct object, first of all, you do not need to change any prototypes.

0
source

All Articles