extjs类的设计
Manual:Intro:Class Design cn
From Learn About the Ext JavaScript Library
| Summary: 类设计 |
| Author: Maxence Delannoy, min.meshy(Translator) |
| Published: Aug 13, 2007 |
| Ext Version: 1.1 |
Languages: English Chinese
|
Contents[hide] |
类的设计
Javascript与其他的面向对象语言不同,如C++,Java或PHP等。它并不是基于类的,而是基于原型的一种语言。
对象创建
在Javascript中创建一个类是非常容易的:
var myObject = { aVar: 15, aMethod: function() { alert("I'm a method of the object myObject." + "aVar: " + this.aVar); } }
你不必通过定义一个类然后实例化该类来创建一个对象。我们在这里使用了一个对象构造器。它满足了使用单个对象的场合。如果我们需要使用同一个类型的多个对象,我们必须使用一个构造器函数和new关键字。
使用构造器函数
在Javascript中没有类的概念,但是构造器是存在的。你可以编写一个函数,然后通过new关键字来创建一个对象。
// 首先,我们为我们的类定义一个空的构造器 function myClass() { this.aVar = 15; this.aMethod = function() { alert("I'm a method of the object myObject."); } } // 创建类的实例 var A = new myClass(); // 显示 15 alert(A.aVar); // 第二个实例 var B = new myClass();
方法共享
你必须使用prototype对象:
// 我们定义了一个prototype对象的一个方法 myClass.prototype.sharedMethod = function() { alert("I'm a shared method") } // 显示我们的信息 A.sharedMethod(); // 相同的信息 B.sharedMethod();
There is no method named sharedMethod in the myClass definition. Javascript looks for a method with this name in the prototype object of myClass and calls it if it exists. 在myClass定义中并没有一个名为sharedMethod的方法。Javascript会在myClass相关联的prototype对象中寻找与该方法名相同的方法,如果存在的话,Javascript则调用该方法。
keim,毕业于安徽科技学院理学院,2003年开始对Web开发有浓厚的兴趣,并专注于C#/java Web开发,软件架构设计、分布式相关、项目管理、前端设计等等,实战派...

Chinese
浙公网安备 33010602011771号