创建构造函数的几种方式

 1 <script>
 2 //    创建构造函数的几种方式:
 3 //1.传入参数
 4    function Product(name){
 5        this.name=name;
 6    }
 7     Product.prototype={};
 8     var iphone=new Product('iphone9s');
 9     console.log(iphone.name);//iphone9s;
10 //2.设置默认值
11     function Product(){
12         this.name='';
13         this.price=0;
14     }
15     Product.prototype={}
16     var iphone=new Product();
17     iphone.description="111";
18     iphone.images=[];
19 //3.传入参数和设置默认值混合
20     function Product(name,price) {
21         this.name = name;
22         this.price = price;
23         this.version = 1.0;
24         this.add = function () {};
25     };
26     Product.prototype={};
27 //    4.动态添加形式
28     var iphone=new Product();
29     iphone.description='111';
30     iphone.images=[];
31     console.log(iphone instanceof Product)//true
32 </script>
原文地址:https://www.cnblogs.com/yangguoe/p/7987031.html