length prototype 函数function的属性,以及构造函数

前言:学到一些JavaScript高级的知识,在这里记下,方便以后的查找

1.length代表函数定义的形参的个数,挺简单的
    例如:function Pen(price,cname) {  ......}
               alert(Pen.length) ;     显示为2
 
2.prototype(原型)这个很重要,它是保存某个对象【所有实例】【共享属性】的地方
     要说明prototype,首先我们引入构造函数,其实跟C#是差不多
1          function Dog(name,age) {
2             this.name = name;
3             this.age = age;
4             this.bark = function () {
5                 alert(this.name+","+this.age);
6             }
7             }
8         }
好了,下面我们来说明prototype
        如果我们下面要new 3个dog对象,其实他们的bark属性方法都是一样的,这就造成资源的浪费,我们可以设置prototype原型,这样的话,3个对象都可以共用这个属性方法,减少资源的浪费
      
 1    //01通过构造函数来创建对象,这里dog对象有个bark属性,这样的话,如果下面new 3个对象,那么这3个对象都创建了3个bark属性的方法挺耗费资源,解决方案
 2         function Dog(name,age) {
 3             this.name = name;
 4             this.age = age;
 5             //this.bark = function () {
 6             //    alert(this.name+","+this.age);
 7             //}
 8             //解决方案~使用prototype原型,这样的话就可以3个对象共用这个属性方法
 9             Dog.prototype.bark = function () {
10                 alert( this.name + "," + this.age);
11             }
12         }
13  
14          var d1 = new Dog("xhh", 12);
15         var d2 = new Dog("02", 12);
16         var d3 = new Dog("03", 12);
原文地址:https://www.cnblogs.com/xhhha/p/3344801.html