当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript object oriented 面向对象编程初步

Javascript
编辑浪子版表单验证类
JavaScript脚本语言在网页中的简单应用
getElementById在任意一款浏览器中都可以用吗的疑问回复
“增强js程序代码的健壮性”之我见大量示例代码
javascript之卸载鼠标事件的代码
静态页面下用javascript操作ACCESS数据库(读增改删)的代码
javascript操作文本框readOnly
用javascript实现gb2312转utf-8的脚本
网站被黑的假象--ARP欺骗之页面中加入一段js
(JS实现)MapBar中坐标的加密和解密的脚本
几款极品的javascript压缩混淆工具
[原创]由亿起发(eqifa.com)的页面发现顶部的http://16a.us/8.js想到的js解密
JavaScript中的new的使用方法与注意事项
分别用两个函数实现的菜单
非常不错的模拟打字效果,目前仅支持纯文本、BR标签、和P标签
用javascript实现的支持lrc歌词的播放器
JS加ASP二级域名转向的代码
效果直逼flash的Div+Css+Js菜单
input之怎么清除默认值
用xhtml+css写的相册自适应 - 类似九宫格[兼容 ff ie6 ie7 opear ]

Javascript 中的 javascript object oriented 面向对象编程初步


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 142 ::
收藏到网摘: n/a

用 new Object() 来创建对象
在javascript里有几种创建对象的方法,在不同的场合可用不同的方法.最简单的就是用 new 操作符,例如:
复制代码 代码如下:
<script language="javascript" type="text/javascript">
<!--
person = new Object()
person.name = "Tim Scarfe"
person.height = "6Ft"
person.run = function() {
this.state = "running"
this.speed = "4ms^-1"
}
//-->
</script>

我们在这个例子里定义了person这个对象,然后加入了它的属性和方法.在这个例子里,自定义的方法里有两个属性.
用文字记号Literal Notation创建对象
用文字记号也可以创建对象,但要javascript 1.2以上版本.它的创建形式是多样的.
复制代码 代码如下:
<script language="javascript" type="text/javascript">
<!--
// Object Literals
timObject = {
property1 : "Hello",
property2 : "MmmMMm",
property3 : ["mmm", 2, 3, 6, "kkk"],
method1 : function(){alert("Method had been called" + this.property1)}
};
timObject.method1();
alert(timObject.property3[2]) // will yield 3
var circle = { x : 0, y : 0, radius: 2 } // another example
// nesting is no problem.
var rectangle = {
upperLeft : { x : 2, y : 2 },
lowerRight : { x : 4, y : 4}
}
alert(rectangle.upperLeft.x) // will yield 2
//-->
</script>

文字记号可是是数组,也可以是任意的javascript表达式或值.
用 new 操作符或文字记号创建一个自定义对象都是简单的,也是符合逻辑的.但它最大的缺点就是结果不可复用.也不能很容易的用不同的版本初始化创建对象.例如上面 的第一个例子,如果 person 的 name 不是 "Tim Scarfe",那样我们不得不重新定义整个对象,仅仅为了适应它的一点点改变.
对象的构造和原型
在OOP的世界里,用先前的方法定义对象在许多场合都有限制.我们需要一种创建对象的方法,类型可以被多次使用而不用重新定义.对象在实例化时每次都可以按需分配不同的值.实现这个目标的标准方法是用对象构造器函数.
一个对象构造器只不过是个有规则的javascript函数,它就象一个容器(定义参数,调用其他函数等等).它们两者所不同的是构造器函数是由 new 操作符调用的.(你将在下面的例子中看到).基于函数语法形式的对象定义,我们可以使它工作得最好.

让我们用现实世界中的猫来举个例子.猫的 name 和 color 是猫的属性.meeyow(猫叫)是它的一个方法.重要的是任何不同的猫都可能有不同的 name 和 meeyow 的叫声.为了建立适应这些特征的对象类,我们将使用对象构造器.
复制代码 代码如下:
<script language="javascript" type="text/javascript">
<!--
function cat(name) {
this.name = name;
this.talk = function() {
alert( this.name + " say meeow!" )
}
}
cat1 = new cat("felix")
cat1.talk() //alerts "felix says meeow!"
cat2 = new cat("ginger")
cat2.talk() //alerts "ginger says meeow!"
//-->
</script>

在这里,函数 cat() 是一个对象构造器,它的属性和方法在函数体里用this来定义,用对象构造器定义的对象用 new 来实例化.主意我们如何非常容易的定义多个cat 的实例.每一个都有自己的名字,这就是对象构造器带给我们的灵活性.
构造器建立了对象的蓝图.并不是对象本身.
在原型里增加方法.
在上面我们看到的例子里,对象的方法是在构造器里定义好的了.另外一种实现的途径是通过原型(prototype).xxx
原型是javascript继承的一种形式.我们可以为对象定义好后,再创造一个方法.原来所有对象的实例都将共享.
让我们来扩展最初的 cat 对象.增加一个改名的方法.用 prototype 的方式.
复制代码 代码如下:
<script language="javascript" type="text/javascript">
<!--
cat.prototype.changeName = function(name) {
this.name = name;
}
firstCat = new cat("pursur")
firstCat.changeName("Bill")
firstCat.talk() //alerts "Bill says meeow!"
//-->
</script>

就象你所看到的.我们仅只用了 关键字 prototype 实现了在对象定义后马上增加了changeName方法.这个方法被所有的实例共享.
用原型(prototype) 重载 javascript 对象
原型 在自定义对象和有选择性的重载对象上都可以工作.比如 Date() 或 String 这可能是无止境的.
子类和超类
在JAVA 和C++里,有关于类层次的外在概念.每一个类能有一个角色.
In Java and C++, there is an explicit concept of the class hierarchy. i.e. Every class can have a super class from which it inherits properties and methods. Any class can be extended, or sub-classed so the resulting subclass can inherit its parent's behavior. As we have seen, javascript supports prototype inheritance instead of class based. It's possible for inheritance to happen other ways, however.
The following is an example of inheritance through functions.
下面一个例子演示了如何继承通过function .
复制代码 代码如下:
<script language="javascript" type="text/javascript">
<!--
// thanks to webreference
function superClass() {
this.supertest = superTest; //attach method superTest
}
function subClass() {
this.inheritFrom = superClass;
this.inheritFrom();
this.subtest = subTest; //attach method subTest
}
function superTest() {
return "superTest";
}
function subTest() {
return "subTest";
}

var newClass = new subClass();
alert(newClass.subtest()); // yields "subTest"
alert(newClass.supertest()); // yields "superTest"
//-->
</script>

基于继承的原型是遥远的.为 javascript 应用程序在许多场合.
(基于原型的继承在许多javascript的应用场合是非常有用的.)
对象作为联合数组
正如你所知, (.)操作符能够用来存储.[] 操作符用来操作数组.
<script language="javascript" type="text/javascript">
<!--
// These are the same
object.property
object["property"]
//-->
</script>
<SCRIPT LANGUAGE="javascript">
<!--
function Circle (xPoint, yPoint, radius) {
this.x = xPoint;
this.y = yPoint;
this.r = radius;
}
var aCircle = new Circle(5, 11, 99);
alert(aCircle.x);
alert(aCircle["x"]);
//-->
</SCRIPT>
How do I loop through properties in an object?
You need to use a for/in loop.
我们可以通过for in循环来遍历对象的属性。
<script language="javascript" type="text/javascript">
<!--
var testObj = {
prop1 : "hello",
prop2 : "hello2",
prop3 : new Array("hello",1,2)
}
for(x in testObj) alert( x + "-" + testObj[ x ] )
//-->
</script>
<SCRIPT LANGUAGE="javascript">
<!--
var Circle = { x : 0, y : 1, radius: 2 } // another example
for(p in Circle)
alert( p + "-" + Circle[ p ] )
//-->
</SCRIPT>

The important thing to notice is that in the object syntax the property is an identifier, whereas in the array syntax, it's a string. The obvious benefits of using an array syntax to access an object is because of the literal data type, you can easily concat strings and play around with them to access an object. For this to work with the standard syntax, an eval() would need to be used.
应该值得注意的是对象的属性只是一个标识字符,尽管在一个数组里是一个字符串,因为是一个literal的数据类型,所以有利于使用数组的方式的操作一个对象。你也可以很容易的存取一个对象在标准的语句中。这个时候eval()函数可能用得到。
<script language="javascript" type="text/javascript">
<!--
testObj = {
prop1 : "hello",
prop2 : "hello2",
prop3 : new Array("helloa",1,2)
}
for(x in testObj) alert( x + "-" + testObj[ x ] )
var prop3 = testObj["prop3"];
alert(prop3);
//alert(prop[1]);
alert(typeof(prop3));
alert(eval(prop3)[1]);
alert(typeof(eval(prop3)[1]));
//-->
</script>
网上的东西错误的太多了,ruanchen.com修正后的测试下
点击运行可以看到效果:
[Ctrl+A 全选 提示:你可先修改部分代码,再按运行]