javascript: 基于原型的面向对象编程

来源:互联网 发布:怎样查看淘宝信誉等级 编辑:程序博客网 时间:2024/05/01 21:21

Douglas Crockford指出javascript是世界上最被误解的编程语言。由于javascript缺少常见的面向对象概念,许多程序猿认为javascript不是一个合适的语言。我在做第一个javascript项目时候也发现不能将代码放在一个类中。其实大部分程序猿不知道javascript可以面向对象。
浏览器大战时代,Netscape的执行官招来了一个叫Brendan Eich的聪明人,发明了livescript(就是现在的javascript)语言,用来运行在浏览器端。它不像c++和java一样基于类,而是基于原型继承模型设计的。 OOP非常适合livescript这种动态语言。
由于当时市场影响,这种新的语言需要看起来像java。java在当时非常耀眼流行,Netscape的执行官希望新语言是”java的小兄弟“。这就是为啥新语言叫javascript。

JavaScript and prototype-based OOP

下面是javascript的oop例子,先来搞出一个animal的对象:

var genericAnimal = Object.create(null);

Object.create(null)创建空对象。下面往这个新对象上添加新属性和方法:

genericAnimal.name = 'Animal';genericAnimal.gender = 'femal';genericAnmal.description = function () {    return 'Gender: ' + this.gender + '; Name: ' + this.name;};

genericAnimal就是一个对象,可以这样使用:

console.log(genericAnimal.description());

我们可以使用genericAnimal作为原型,创建其他对象,就像拷贝对象(cloning the object):

var cat = Object.create(genericAnimal);

我们创建了cat对象,cat对象是genericAnimal的clone版。将cat对象作为原型,创建其他对象:

var colonel = Object.create(cat);colonel.name = 'Colonel Meow';var puff = Object.create(cat);puff.name = 'Puffy';

puff对象可以使用“父对象”继承来的属性和方法:

console.log(puff.description());//Gender: female; Name: Puffy

The new keyword and the constructor function

javascript有new关键字和构造函数的感念。

function Person(name) {    this.name = name;    this.sayName = function() {        return "Hi, I'm " + this.name;    };}var adam = new Person('Adam');

实现继承有点复杂, Ninja对象继承Person对象:

function Ninja(name, weapon) {  Person.call(this, name);  this.weapon = weapon;}Ninja.prototype = Object.create(Person.prototype);Ninja.prototype.constructor = Ninja;

Understanding delegation and the implementation of prototypes

使用Object.create创建新对象时候,传入的对象成了新对象的原型。每个对象都有默认的__proto__属性来记录原型。举例说明:

var genericAnimal = Object.create(null); // genericAnimal.__proto__ points to null

使用genericAnimal作为原型创建一个新的空对象:

var rodent = Object.create(genericAnimal); rodent.size = 'S'; var capybara = Object.create(rodent);//capybara.__proto__ points to rodent//capybara.__proto__.__proto__ points to genericAnimal//capybara.__proto__.__proto__.__proto__ is null

capybara对象虽然没有size属性,但是它可以在原型链上查找size属性:当调用capybara.size时,首先在capybara对象的本身属性上查找,如果没有找到,则会去capybara.__proto__所指向的原型上去查找。如果在capybara.__proto__也没有找到,则继续在capybara.__proto__.__proto__上查找。

Creating Object.create

如果有些浏览器不支持Object.create,那么需要自己实现了:

 if (typeof Object.create !== 'function') {    Object.create = function (o) {        function F() {}        F.prototype = o;        return new F();    };}
0 0
原创粉丝点击