javascript object oriented 面向对象编程初步

所属分类: 网络编程 / JavaScript 阅读数: 1071
收藏 0 赞 0 分享
用 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>
网上的东西错误的太多了,jb51.net修正后的测试下
更多精彩内容其他人还在看

Angular使用Md5加密的解决方法

这篇文章主要介绍了Angular使用Md5加密的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解JS构造函数中this和return

本文通过实例代码给大家介绍了JS构造函数中this和return,需要的朋友参考下吧
收藏 0 赞 0 分享

ES6中Array.find()和findIndex()函数的用法详解

ES6为Array增加了find(),findIndex函数。find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined,而findIndex()函数也是查找目标元素,找到就返回元素的位置,找不到就返回-1。下面通过实例详解,需要的朋友参考下吧
收藏 0 赞 0 分享

JS闭包的几种常见形式实例详解

本文通过实例代码给大家详细介绍了js闭包的几种常见形式,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下
收藏 0 赞 0 分享

ES6中Array.copyWithin()函数的用法实例详解

ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去。下面重点给大家介绍ES6中Array.copyWithin()函数的用法,需要的朋友参考下
收藏 0 赞 0 分享

Javascript 严格模式use strict详解

严格模式:由ECMA-262规范定义的JavaScript标准,对javascrip的限制更强。这篇文章主要介绍了Javascript 严格模式use strict详解 ,需要的朋友可以参考下
收藏 0 赞 0 分享

引入JavaScript时alert弹出框显示中文乱码问题

今天在HTML中引入JavaScript文件运行时,alert弹出的提示框中文显示为乱码,怎么解决此问题呢?下面小编给大家带来了引入JavaScript时alert弹出框显示中文乱码问题的解决方法,一起看看吧
收藏 0 赞 0 分享

AngularJs 延时器、计时器实例代码

这篇文章主要介绍了AngularJs 延时器、计时器实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

JS分页的实现(同步与异步)

这篇文章主要介绍了JS分页的实现(同步与异步),需要的朋友可以参考下
收藏 0 赞 0 分享

Angularjs自定义指令实现分页插件(DEMO)

由于最近的一个项目使用的是angularjs1.0的版本,涉及到分页查询数据的功能,后来自己就用自定义指令实现了该功能,下面小编把实例demo分享到脚本之家平台,需要的朋友参考下
收藏 0 赞 0 分享
查看更多