javascript的对象与原型

1.  创建对象的3种方式

	 //1 .工厂模式创建对象
    function createObject(name,age) {
      var obj = new Object();//创建对象
      //添加属性
      obj.name = name;
      obj.age = age;
      //添加方法
      obj.sayHi = function () {
        console.log("阿涅哈斯诶呦,我叫:" + this.name + "我今年:" + this.age);    // 在function中可以用this,this代表这个对象
      };
      return obj;
    }
    //创建人的对象
    var per1 = createObject("小芳",20);
    per1.sayHi();
    //创建一个人的对象
    var per2 = createObject("小红",30);
    per2.sayHi();

    console.log(per1 instanceof Object)  // true
    console.log(per2 instanceof Object)	 // true
	
	
	//2. 自定义构造函数创建对象,我要自己定义一个构造函数,自定义构造函数,创建对象
    //函数和构造函数的区别;名字是不是大写(首字母是大写)
	
	    //自定义构造函数创建对象:先自定义一个构造函数,创建对象
	    /*
    *自定义构造函数创建对象做了以下4件事
    * 1. 在内存中开辟(申请一块空闲的空间)空间,存储创建的新的对象
    * 2. 把this设置为当前的对象
    * 3. 设置对象的属性和方法的值
    * 4. 把this这个对象返回
    *
    *
    * */
	
    function Person(name,age) {
      this.name=name;
      this.age=age;
      this.sayHi=function () {
        console.log("我叫:"+this.name+",年龄是:"+this.age);
      };
    }


    var obj=new Person("小明",10);
    console.log(obj.name);
    console.log(obj.age);
    obj.sayHi();

    var obj2=new Person("小红",20);
    console.log(obj2.name);
    console.log(obj2.age);
    obj2.sayHi();

    console.log(obj instanceof Person); // true  自定义的可以用来看属于哪个对象了
    console.log(obj2 instanceof  Person); // true

	//3. 字面量的方式创建对象
	var obj={};
    obj.name="小白";
    obj.age=10;
    obj.sayHi=function () {
      console.log("我是:"+this.name);
    };
    obj.sayHi();
	// 优化后,推荐
    var obj2={
      name:"小明",
      age:20,
      sayHi:function () {
        console.log("我是:"+this.name);
      },
      eat:function () {
        console.log("吃了");
      }
    };
    obj2.sayHi(); // 或者可以obj2['sayHi']()
    obj2.eat();
    //总结: javascript高级/01/06
    /*
    * 实例对象和构造函数之间的关系:
    * 1. 实例对象是通过构造函数来创建的---创建的过程叫实例化
    * 2.如何判断对象是不是这个数据类型?
    *  1) 通过构造器的方式 实例对象.构造器==构造函数名字
    *  2) 对象 instanceof 构造函数名字
    *  尽可能的使用第二种方式来识别,为什么?原型讲完再说
    *
    *
    *
    * */
	//自定义构造函数----->实例化对象
    function Person(name,age,sex) {
      this.name=name;
      this.age=age;
      this.sex=sex;
      this.eat=function () {
        console.log("吃大蒜拌臭豆腐加榴莲酱");
      };
    }
    //构造函数---->创建对象
    var per=new Person("小苏",38,"女");
	
    console.log(per.constructor==Person);// true

    console.log(per.__proto__.constructor==Person); // true
    console.log(per.__proto__.constructor==Person.prototype.constructor); // true

2 原型的引入

function have(){
  console.log('我在吃菜')
}

// var have = 10;
function  Person(name,age){
  this.name = name;
  this.age = age;
  this.eat = function(){
    console.log("我在吃饭")
  };
  this.have = have
}



Person.prototype.run = function(){
  console.log('我在跑步')
}

p1 = new Person('lh',11)
p2 = new Person('ll',12)

console.log(p1.eat == p2.eat)  // false 说明存储的不是一个对象,怎么才能是一个对象来节省空间
console.log(p1.have == p2.have)  // true, 这有个不好是会存在变量被覆盖的情况,比如var have = 10;那么p1.have 就为10了

//应该用原型解决这2个问题
console.log(p1.run == p2.run) // true

猜你喜欢

转载自blog.csdn.net/qq_34964399/article/details/81779871