search

Home  >  Q&A  >  body text

javascript - What is the use of constructor:Plugin statement in jq plug-in?

I encountered problems in the case I referred to when learning jquery custom plug-in development.

女神的闺蜜爱上我女神的闺蜜爱上我2744 days ago744

reply all(1)I'll reply

  • 为情所困

    为情所困2017-06-12 09:31:41

    Point the constructor to itself, otherwise it will point to Object by default

    var Plugin = function(){}
    Plugin.prototype = {
        sayHello:function(){
            console.log("hello")
        }
    }
    var p = new Plugin()
    console.log(p.constructor === Object) //true
    

    If you add constructor

    var Plugin = function(){}
    Plugin.prototype = {
        constructor: Plugin,
        sayHello:function(){
            console.log("hello")
        }
    }
    var p = new Plugin()
    console.log(p.constructor === Plugin) //true
    

    reply
    0
  • Cancelreply