构造函数及其包装类

2018-08-01  本文已影响0人  浮巷旧人

对象

都得大写

function Car(color) {
        this.color = color;
        this.name = "BMW";
        this.height = "1400";
        this.lang =  "4900";
        this.weight = 1000;
        this.health = 100;
        this.run = function () {
          this.health --;
        }
 }
var  car = new  Car('red');通过参数来改变函数 发
生自定义环节
 var  car1 = new Car('green');
 car.name = "Maserati";
 car1.name = " Merz";两个name互不影响
function Student (name,age,sex){
      //var this = {name : ""   age:"" };
     this.name = name;
     this.age = age;
     this.sex = sex;
     this.grade = 2017;
      //return this;
  }
 var  student = new Student ('zhangsan',  18,  'male');
console.log(new Student ('zhangsan',  18,  'male').name);
var str = "abc";
/new String('abc').length
console.log(str.length)    ->3
正常来讲原始值str是没有属性的,但是程序为了
不报错,隐式的进行了一个操作,于是构造了一个
字符串对象的构造函数new String( ),然后将内
容‘abc’放在里面,把这个对象进行 . length操作,这
个过程就是包装类(主要是原始值没有属性和方
法,但是生成得对象可以有属性)
var num = 123;
num.abc  =  “abc”
/new Number(num).abc  =  'abc';    ->delete(为了
不让它报错 执行完就删除了)
/两个长得一样,彼此独立,上面那个已经被销毁
了,下面那个是重新建立得
/new Number(num).abc 
console.log(num.abc)    ->underfined
上一篇 下一篇

猜你喜欢

热点阅读