小编Nes*_*tez的帖子

创建一个JS类:IIFE vs返回原型

让我们看两个例子,我将尝试解释我想要理解的内容.

var Car = function(){
  // Init class
  function Car() { };
  // Private func/vars
  var private = { color:'red' };
  // Public func/vars
  Car.prototype = {
    newColor: function(color) { private.color = color },
    getColor: function() { return private.color }
  };

  return Car.prototype; // return with prototype
};

var myCar = new Car();
Run Code Online (Sandbox Code Playgroud)

和:

var Car = (function(){
  // Init class
  function Car() { };
  // Private func/vars
  var private = { color:'red' };
  // Public func/vars
  Car.prototype = { …
Run Code Online (Sandbox Code Playgroud)

javascript prototype class iife

17
推荐指数
1
解决办法
5091
查看次数

标签 统计

class ×1

iife ×1

javascript ×1

prototype ×1