- <SCRIPT LANGUAGE="JavaScript">
- /*
- 1: 类的定义:基本概念
- */
- function Person(name) { // 类
- //实例属性
- this.name = name;
- //实例方法
- this.helloWorld = function(){
- alert("hello world!");
- };
- }
- //静态属性
- Person.prototype.sayHello = "you are pig!";
- //静态方法
- Person.prototype.helloWorldToo = function () {
- alert("hello world too!");
- }
- //类属性
- Person.working = "yes";
- //类方法
- Person.say = function () {
- alert("hello world!");
- }
- //调用:
- var XiaoWang = new Person("小王");//实例化 --- 一个新的小王出生了。
- alert( XiaoWang.name ); //调用实例属性 ---- 我们想知道他的名字
- XiaoWang.helloWorld(); //调用实例方法 ---- 小王开始说 的第一句话。
- var XiaoLiu = new Person("小刘");//实例化 --- 一个新的小刘出生了。
- alert( XiaoLiu.name ); //调用实例属性 ---- 我们想知道他的名字.
- XiaoLiu.helloWorld(); //调用实例方法 ---- 小刘开始说 的第一句话。
- alert( XiaoWang.sayHello ); // 调用静态属性 --- 小王开始骂人了。
- alert( XiaoLiu.sayHello ); // 调用静态属性 --- 小刘开始骂人了。
- XiaoWang.helloWorldToo() ; // 调用静态方法 --- 小王说的第三句话。
- XiaoLiu.helloWorldToo() ; // 调用静态方法 --- 小刘说的第三句话。
- alert( Person.working ); //调用类的属性 --- 不是 XiaoWang.working
- Person.say(); //调用类的方法 --- 不是 XiaoWang.say()
- </SCRIPT>
下一篇:js2--实例属性和实例方法

关于本站 - 免责声明 - 友情链接 - 提点建议 - 联系我们 - 链接纠错 - 广告投放
Copyright © 2008 All rights reserved.