亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

設置函數構造函數原型(示例中)

設置函數構造函數原型(示例中)

Smart貓小萌 2023-08-18 17:06:27
我試圖理解“JavaScript:理解奇怪的部分”課程中的一個例子。有一行代碼Greeter.init.prototype = Greeter.prototype;用于使函數構造函數Greeter.prototype創建的所有對象成為原型Greeter.init,因此我們可以在Greeter.prototype.但我不明白為什么不直接在Greeter.init.prototype. 效果是一樣的。該行Greeter.init.prototype = Greeter.prototype;看起來像是多余的代碼。原始方法的優點是什么?原始代碼:(function(global, $) {      var Greetr = function(firstName, lastName, language) {      return new Greetr.init(firstName, lastName, language);     }  Greetr.prototype = {    fullName: function() {      return this.firstName + ' ' + this.lastName;    }  };  Greetr.init = function(firstName, lastName, language) {          var self = this;    self.firstName = firstName || '';    self.lastName = lastName || '';    self.language = language || 'en';         }  Greetr.init.prototype = Greetr.prototype;  global.Greetr = global.G$ = Greetr;    }(window, jQuery));var g = G$('John', 'Doe');console.log(g);console.log(g.fullName());<html>    <head>            </head>    <body>        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>    </body></html>簡化代碼:(function(global, $) {      var Greetr = function(firstName, lastName, language) {      return new Greetr.init(firstName, lastName, language);     }  Greetr.init = function(firstName, lastName, language) {          var self = this;    self.firstName = firstName || '';    self.lastName = lastName || '';    self.language = language || 'en';         }  Greetr.init.prototype = {    fullName: function() {      return this.firstName + ' ' + this.lastName;    }  };  global.Greetr = global.G$ = Greetr;    }(window, jQuery));var g = G$('John', 'Doe');console.log(g);console.log(g.fullName());<html>    <head>            </head>    <body>        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>    </body></html>
查看完整描述

1 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

由于Greetr構造函數調用Greetr.init構造函數,并通過顯式返回對象來覆蓋其返回值,因此它們之間沒有區別。

這兩段代碼創建完全相同的對象結構并以相同的方式工作,但有一點不同:第二段代碼保持Greetr.prototype其初始狀態。

然而,最有可能的是,原始代碼對同一個對象進行了創建Greetr.prototype,Greetr.init.prototype因為這樣就可以在不輸入 的情況下訪問和/或擴展它.init,這更具語義性:您打算更改由 所創建的對象的原型Greetr,其原型通常是Greetr.prototype。另外,在第一個代碼中,instanceof將把由Greetr和創建的對象視為Greetr.init的實例Greetr。所以:

(function(global, $) {

    

  var Greetr = function(firstName, lastName, language) {

      return new Greetr.init(firstName, lastName, language);   

  }


  Greetr.prototype = {

    fullName: function() {

      return this.firstName + ' ' + this.lastName;

    }

  };


  Greetr.init = function(firstName, lastName, language) {      

    var self = this;

    self.firstName = firstName || '';

    self.lastName = lastName || '';

    self.language = language || 'en';       

  }


  Greetr.init.prototype = Greetr.prototype;


  global.Greetr = global.G$ = Greetr;

    

}(window, jQuery));



var g = G$('John', 'Doe');

console.log(g instanceof G$);

<html>

    <head>

        

    </head>

    <body>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    </body>

</html>


(function(global, $) {

    

  var Greetr = function(firstName, lastName, language) {

      return new Greetr.init(firstName, lastName, language);   

  }


  Greetr.init = function(firstName, lastName, language) {      

    var self = this;

    self.firstName = firstName || '';

    self.lastName = lastName || '';

    self.language = language || 'en';       

  }


  Greetr.init.prototype = {

    fullName: function() {

      return this.firstName + ' ' + this.lastName;

    }

  };


  global.Greetr = global.G$ = Greetr;

    

}(window, jQuery));



var g = G$('John', 'Doe');

console.log(g instanceof G$);

<html>

    <head>

        

    </head>

    <body>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    </body>

</html>


查看完整回答
反對 回復 2023-08-18
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號