JavaScript-面向对象
一、JavaScript面向对象语言特性:
*传统的面向对象编程语言中,通常对象是由类派生而来的,而类定义来了成员属性和成员方法。
*JavaScript编程语言中没有类的概念,它是一门基于原型(prototype)的面向对象编程的语言。
*JavaScript面向对象这个词其实是多余的,因为JavaScript这门语言就是完全面向对象的。
*对象是组成Javascript编程语言的基本单元,JavaScript编程语言中的一切都是对象。
二、JavaScript对象化能力
*在JavaScript语言中只有object和function两种东西有对象化的能力。
<script type="text/javascript">
//<![CDATA[
var NoteBook = {} //创建一个笔记本对象
NoteBook.name = "MacBook Pro";
NoteBook.model = "MC374";
NoteBook.say = function(){
document.write("<h1>计算机的名称是" + this.name + "型号是" + this.model + "</h1>")
}
NoteBook.say();
//NoteBook['say']();
var NoteBook = function(){} //创建一个笔记本函数对象
NoteBook.name = "SAMSUNG";
NoteBook.model = "B002";
NoteBook.say = function(){
document.write("<h1>计算机的名称是" + this.name + "型号是" + this.model + "</h1>")
}
NoteBook['say']();
//]]>
</script>
说明:
所有 XML 文档中的文本均会被解析器解析。
只有 CDATA 区段(CDATA section)中的文本会被解析器忽略。
For instance:
以下的例子展示的是青蛙的进化过程.
<script type="text/javascript">
//<![CDATA[
var life = {}; //光溜溜的生命对象,这种方式相当于是var life = new Object();
for(life.age = 1;life.age<=4;life.age++){
switch(life.age){
case 1:
life.body = "卵细胞";
life.say = function(){
document.write("<h1>" + this.age + this.body + "</h1>")
};
break;
case 2:
life.tail = "尾巴";
life.gill = "腮";
life.body = "蝌蚪";
life.say = function(){
document.write("<h1>" + this.age + this.body + " - " + this.tail + "," + this.gill + "</h1>");
};
break;
case 3:
delete life.tail;
delete life.gill;
life.legs = "四条腿";
life.lung = "肺";
life.body = "青蛙";
life.say = function(){
document.write("<h1>" + this.age + this.body + " - " + this.legs + "," + this.lung + "</h1>");
};
break;
case 4:
life.arm = "两只手臂";
life.legs = "两条腿";
life.lung = "肺";
life.body = "青蛙王子";
life.say = function(){
document.write("<h1>" + this.age + this.body + " - " + this.arm + "," + this.legs + "," + this.lung + "</h1>");
};
break;
}
life.say();
}
//]]>
三、JavaScript对象表示法
<script type="text/javascript">
//<![CDATA[
/*
JSON为创建对象提供了简单的方法,JSON(JavaScript Object Notation),中文称JavaScript对象表示法
*/
//创建一个没有任何属性的对象
var o = {};
//创建一个对象并设置属性及初始值
var person = {name: "Angel", age: 18, married: false};
//创建一个对象并设置属性和方法
var speaker = {text: "Hello World", say: function(){alert(this.text)}};
//创建一个更复杂的对象,嵌套其他对象和对象数组等
var company = {
//成员属性
name: "Microsoft",
product: "softwares",
//对象
chairman: {name: "Bill Gates", age: 53, Married: true},
//对象数组 employees: [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}],
//成员方法
readme: function() {document.write(this.name + " product " + this.product);}
};
//]]>
</script>
四、php 与JavaScript 的面向对象的实现:
<?php
//人类
class Person{
public $name;
public $age;
public $sex;
public function __construct($name, $age, $sex){
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public function say(){
echo "<h1>PHP".$this->name." - ".$this->age." - ".$this->sex."</h1>";
}
}
$p = new Person("于亚东",25,"男");
$p->say()
?>
<script type="text/javascript">
//<![CDATA[
//定义一个构造函数相当于类
function Person(name, age, sex){
this.name = name;
this.age = age;
this.sex = sex;
this.say = function(){
document.write("<h1>JavaScript" + this.name + " - " + this.age + " - " + this.sex + "</h1>");
}
}
var p = new Person("刘悦伶",23,"女");
p.say();
//]]>