search

Home  >  Q&A  >  body text

javascript - If react sets this.id = XXX in the constructor, what is the use of this id?

For example, the id here, why is it defined like this? I see there is no such definition on the Internet, or I can't find it. Please answer. If there is a relevant link, please provide a link. Thank you all

过去多啦不再A梦过去多啦不再A梦2796 days ago595

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:41:08

    Setting properties in the constructor has no special meaning.
    If this writing method is not ES6, it will be easier for you to understand.

    function Parent(){
        this.id = +new Date()
    }
    
    Parent.prototype.getId = function(){
        console.log(this.id)
    }
    
    var  p = new Parent()
    p.getId() // 其实这里定义也就是给当前类,定义属性而已。便于后面在当前类里面使用。

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-19 10:41:08

    The usefulness depends on the person who writes the program. The React component itself does not have this attribute.

    reply
    0
  • Cancelreply