JavaScript对象基础

JavaScript对象

JavaScript对象是变量,但是它包含很多值。它的值以名称:值对的方式来书写。JavaScript对象是被命名值的容器。

var student = {name: "danjie",age: 21,sex: "male"};

JavaScript对象创建的方式

  • 工厂方式
var oStudent = new Object();
oStudent.name = "danjie";
oStudent.age = 21;
oStudent.sex = "male";
oStudent.speak = "hello";
oStudent.sayWord = function(){
  console.log(this.speak);
}

工厂模式是利用对象的属性可以在创建对象后动态定义的特性,对创建的对象添加属性,但是它在需要创建多个实例时会遇到问题,无法复用,这时需要对它进行改进,我们需要去创建一个工厂函数

function createStudent() {
  var oTempStudent = new Object();
  oTempStudent.name = "danjie";
  oTempStudent.age = 21;
  oTempStudent.sex = "male";
  oTempStudent.speak = "hello";
  oTempStudent.sayWord = function(){
    console.log(this.speak);
  };
  return oTempStudent;
0}

对于创建属性完全相同的两个对象,这样的方法很有用。例如:

var oStudent1 = creatStudent();
var oStudent2 = creatStudent();

但是当我们想要为不同的对象不同的属性时,我们需要修改createStudent()函数,给它传递属性的参数。

function createStudent(sName,sAge,sSex,sSpeak) {
  var oTempStudent = new Object();
  oTempStudent.name = sName;
  oTempStudent.age = sAge;
  oTempStudent.sex = sSex;
  oTempStudent.speak = sSpeak;
  oTempStudent.speakWord = function(){
    console.log(this.speak);
  };
  return oTempStudent;
0}

这时我们只需要给createStudent函数传递不同的参数便可以给不同的对象赋予不同的属性值了。

var oStudent1 = createStudent("danjie",21,"male","hello");
var oStudent2 = createStudent("xiaoming",22,"male","goodbye");

但是即使这样,当我们想要创建一些共用的方法时,却会创建新的speakWord()函数,这也意味着每个对象都有自己的speakWord()版本。那么这时就需要使用在工厂函数外定义对象了。

function speakWord() {
    console.log(this.speak);
}

function createStudent(sName,sAge,sSex,sSpeak) {
  var oTempStudent = new Object();
  oTempStudent.name = sName;
  oTempStudent.age = sAge;
  oTempStudent.sex = sSex;
  oTempStudent.speak = sSpeak;
  oTempStudent.speakWord = speakWord;
  };
  return oTempStudent;
}

这个时候虽然解决了重复创建函数的问题,但是这个函数却不太像对象的方法。这些问题引发了开发者定义的构造函数的出现。

  • 构造函数方式

构造函数使用this指针代替oTempStudent,在执行第一行代码前先创建一个对象,只有用this才可以访问该对象,可以直接赋予this属性,默认情况下不必明确使用return运算符。

function Student(sName,sAge,sSex,sSpeak) {
  this.name = sName;
  this.age = sAge;
  this.sex = sSex;
  this.speak = sSpeak;
  this.speakWord = function(){
    console.log(this.speak);
  }
}

var oStudent1 = new Student("danjie",21,"male","hello");
var oStudent2 = new Student("xiaoming",22,"male","goodbye");

现在使用new方法创建,就更像是创建了一个对象,但是它同样存在重复创建函数的问题,也可以使用工厂模式中的解决方法来解决这个问题,但是这样也是在语义上无意义的。那么我们要如何解决这个问题呢?这就要讲一讲原型模式了。

  • 原型方式

这个方式利用prototype属性,可以将它看成创建新对象所依赖的原型。

首先使用空构造函数来设置类(对象)名,然后将所有的属性和方法赋予prototype属性,像下面这样:

function Student(){
}

Student.prototype.name = "danjie";
Student.prototype.age = 21;
Student.prototype.sex = "male";
Student.prototype.speak = "hello";
Student.prototype.speakWord = function(){
  console.log(this.speak);
};

var oStudent1 = new Student();
var oStudent2 = new Student();

这样解决了函数重复创建的问题,但是问题却在与属性指向的是对象,而不是函数。例如:

function Student(){
}

Student.prototype.name = "danjie";
Student.prototype.age = 21;
Student.prototype.sex = "male";
Student.prototype.speak = "hello";
Student.prototype.friends = new Array("Peter","Jhon");
Student.prototype.speakWord = function(){
  console.log(this.speak);
};

var oStudent1 = new Student();
var oStudent2 = new Student();

oStudent1.friends.push("Amy");

console.log(oStudent1.friends);
console.log(oStudent2.friends);

我们会发现输出都为"Peter,Jhon,Amy"。因为属性friends是指向Array对象的指针,由于friends是引用值,所以Student的两个实例都指向同一个数组。所以会存在只给oStudent1.friends添加值的时候也会影响到oStudent2.friends。

那么要如何同时解决工厂模式,原型模式和构造函数存在的问题呢?

  • 混合的构造函数/原型方式

既然原型模式可以解决重复创建函数的问题,构造函数可以解决无法独立设置属性的问题,那我们可以通过混用这两个方式来创建对象。例如:

function Student(sName,sAge,sSex,sSpeak) {
  this.name = sName;
  this.age = sAge;
  this.sex = sSex;
  Student.prototype.friends = new Array("Peter","Jhon");
  this.speak = sSpeak;
}

Student.prototype.speakWord = function(){
  console.log(this.speak);
}

var oStudent1 = new Student("danjie",21,"male","hello");
var oStudent2 = new Student("xiaoming",22,"male","goodbye");

oStudent1.friends.push("Amy");

console.log(oStudent1.friends);
console.log(oStudent2.friends);

这样就可以解决上面的问题了。也可以利用instanceof运算符来判断对象的类型。这种方式是ECMAScript采用的主要方式

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容