te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>class - JavaScript object output in console.log - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

class - JavaScript object output in console.log - Stack Overflow

programmeradmin3浏览0评论

I want to know from where does console.log get the name of the constructing function when printing an object. Also, does that actually effect anything code wise?

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

var f = new F();

console.log( f );

The output of console.log (in Chrome) is: F {test: "ok"}

Where does the console.log get the 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 still the same - F {test: "ok"}

Is this information is simply kept privately by the browser, my question is does it affect JavaScript code in any way? That is, will it creep up during parison or inheritance, after I override the constructor's prototype and constructor properties?

UPDATE

The original purpose 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 print of dev outputs F {name: "John Doe"}, which made me question whether I may run into problems later on if I'd like to make parisons or inheritance with instances constructed in such a way.

I want to know from where does console.log get the name of the constructing function when printing an object. Also, does that actually effect anything code wise?

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

var f = new F();

console.log( f );

The output of console.log (in Chrome) is: F {test: "ok"}

Where does the console.log get the 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 still the same - F {test: "ok"}

Is this information is simply kept privately by the browser, my question is does it affect JavaScript code in any way? That is, will it creep up during parison or inheritance, after I override the constructor's prototype and constructor properties?

UPDATE

The original purpose 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 print of dev outputs F {name: "John Doe"}, which made me question whether I may run into problems later on if I'd like to make parisons or inheritance with instances constructed in such a way.

Share Improve this question edited Feb 5, 2014 at 23:27 Vadym asked Feb 5, 2014 at 22:44 VadymVadym 9942 gold badges13 silver badges31 bronze badges 1
  • I have added another answer which targets the original problem you had, instead of just trying to get your solution to work. – Johannes H. Commented Feb 5, 2014 at 23:42
Add a ment  | 

5 Answers 5

Reset to default 3

Changing F.prototype replaces the content of F, not the name. The old prototype object still exists and a reference to it is stored internally in each instance of the old F. You cam check it by calling f.__proto__´ (deprecated) or Object.getPrototypeOf(f).

Note that __proto__ is an accessor proterty (internally a getter, not a real property), so it cannot be changed.

It's not difficult, because f is finally an instance of F and the order of scope resolving (this, prototype, ...) is obvious :-)

For example, you can run this code and you'll 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

Your creating a new instance of F, so the browser prints that in order to help you keep track of your logging. Even though you change the prototype would you still have to create a new "F" in order to get the object.

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

object.constructor.name is another way of getting the name of an object constructor.

May I suggest another approach for the original intention? There is no problem with 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's a function, not a class
    return new constructor(args);
}

This should create the right object in the first place, no need to swap any prototypes.

发布评论

评论列表(0)

  1. 暂无评论