console.log中的JavaScript对象输出

Vad*_*dym 6 javascript class new-operator console.log

我想知道console.log在打印对象时从哪里获取构造函数的名称.此外,这实际上是否会影响代码?

function F() { 
    this.test = 'ok';
}

var f = new F();

console.log( f );
Run Code Online (Sandbox Code Playgroud)

console.log(在Chrome中)的输出为:F {test:"ok"}

在什么地方的console.log得到FF {test...

如果我改变F.constructor,F.prototype以及f.constructor随机的东西,它仍然打印原始F:

function G() {
    this.fail = 'bad';
}

function F() { 
    this.test = 'ok';
}

F.prototype = G;
F.constructor = G;

var f = new F();

console.log( f );
Run Code Online (Sandbox Code Playgroud)

输出仍然相同 - F {test: "ok"}

这些信息是由浏览器私下保存的,我的问题是它是否以任何方式影响JavaScript代码?也就是说,在我覆盖构造函数prototypeconstructor属性之后,它会在比较或继承期间爬行吗?

UPDATE

最初的目的是做到以下几点.

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"}`
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,产生的dev输出F {name: "John Doe"}结果让我怀疑,如果我想在以这种方式构造的实例上进行比较或继承,我是否可能在以后遇到问题.

Joh*_* H. 3

更改F.prototype替换的是 的内容F,而不是名称。旧的原型对象仍然存在,并且对它的引用存储在旧的每个实例的内部F。您可以通过调用f.__proto__\xc2\xb4(已弃用)或来检查它Object.getPrototypeOf(f)

\n\n

请注意,这__proto__是一个访问器属性(内部是一个 getter,而不是真正的属性),因此无法更改。

\n