JavaScript高级程序设计12.pdf

第六章 面向对象的程序设计

ECMA中有两种属性:数据属性和访问器属性

数据属性的特性

[[Configurable]]  表示是否通过delete删除属性,是否重新定义属性,是否能把属性修改为访问器属性

[[Enumerable]]  表示是否通过for-in循环返回属性

[[writable]]  表示是否修改属性的值

[[Value]]  包含这个属性的数据值,从这个属性里读取数据值

要修改属性默认的特效必须使用ECMAScript 5的Object.defineProperty()方法,它接收3个参数:属性所在的对象,属性的名字和描述符对象(上面的4个描述符对象的属性)

var person={};

Object.defineProperty(peron,"name",{

  writable:false,

  value:"Nicholas"

  });

alert(person.name);  //"Nicholas"

person.name="Greg";

alert(person.name);  //"Nicholas"

一旦把configurable设置为false,就不能改为true,此后只能更改writable属性

访问器属性

访问器属性不包含数据值;它们包含一对getter和setter函数(两个都不是必须的),有以下4个特性

[[Configurable]]  表示是否通过delete删除属性,是否重新定义属性,是否能把属性修改为数据属性

[[Enumerable]]  表示是否通过for-in循环返回属性

[[Get]]  读取属性时调用的函数,默认为undefined

[[Set]]  写入属性时调用的函数,默认为undefined

用Object.defineProperty()来定义

var book={

  _year:2004,

  edition:1

  };

Object.defineProperty(book,"year",{

  get:function(){

    return this._year;

    },

  set:function(newValue){

    if (newValue > 2004) {

      this._year=newValue;

      this.edition +=newValue-2004;

      }

    }

  });

book.year=2005;

alert(book.edition);  //2

不一定要同时指定getter和setter,严格模式下写入只指定getter函数会抛出异常,非严格模式下返回undefined,反之亦然

定义多个属性 Object.defineProperties()方法

var book={};

Object.defineProperties(book,{

  _year:{

    value:2004},

  edition:{

    value:1},  

  year:{

    get: function(){

        return this_year;},

    set: function(newValue){

        if(newValue>2004){

        this._year=newValue;

        this.edition+=newValue-2004;}

      }

    }

});

读取属性的特性 Object.getOwnPropertyDescriptor()方法

以以上为例

var descriptor=Object.getOwnPropertyDescriptor(book,"_year");

alert(descriptor.value);  //2004

alert(descriptor.configurable);  //false

工厂模式

function createPerson(name,age,job){

  var o =new Object();

  o.name=name;      

  o.age=age;

  o.job=job;

  o.sayName=function(){

      alert(this.name);

        };

  return o;

}

var person1=createPerson("Greg",25,"Doctor");

构造函数模式(自定义构造函数)

function Person(name,age,job){

  this.name=name;      

  this.age=age;

  this.job=job;

  this.sayName=function(){

      alert(this.name);

        };

}

var person1=new Person("Greg",25,"Doctor");

alert(person1 instanceof Person);  //true

alert(person1.constructor==Person);  //true

创建自定义的构造函数可以将它的实例标识为一种特定的类型,这是胜过工厂模式的地方

构造函数是比较特殊的函数,可以不使用new标识符(new其实就是在后台创建一个新对象),this就是指向当前作用域

Person("Greg",27,"Doctor");  //添加到window

window.sayName();  //"Greg"

可以使用call()、apply()

var o=new Object();

Person.call(o,"kristen",25,"Nurse");

o.sayName();  //"kristen"

构造函数模式缺点是每个方法都要在实例上创建一遍,所以每个实例里的同名函数都是不同的

原型模式

实例有一个prototype属性指向函数的原型对象,原型对象有一个constructor属性指向构造函数

 person.prototype()

person.prototype.constructor()

可以通过isPrototypeOf()方法确定对象之间是否存在这种关系

alert(person.prototype.isPrototypeOf(person1));  //true

可以通过Object.getPrototypeOf()返回[[Prototype]]的值

alert(Object.getPrototype(person1)==person.prototype);  //true

在搜索一个对象的属性时,会先搜索实例,如没有再搜索原型,如有则屏蔽原型的属性,delete可以删除实例的属性,从而访问到原型中的属性

delete person1.name;

原文地址:https://www.cnblogs.com/sdgjytu/p/3713708.html