js對象的建立
JS真是一門經典腳本語言,近日看一些JS的面向對象編程的文章,記錄下來。
1.構建類:
??? JS中構建類,其實就是構建類的一個構造方法。如下:
var
?Foo?
=
?
function
?()
????{
???????? this .name? = ? " aaa " ;
???????? this .b? = ? " bbb " ;
???????? this .sayHello? = ? function ?()
????????{
????????????alert( " welcome:?? " + this .name);
????????}
????};
???? var ?foo? = ? new ?Foo();
????foo.name? = ? " wh " ;
????foo.sayHello();
或者如下:????{
???????? this .name? = ? " aaa " ;
???????? this .b? = ? " bbb " ;
???????? this .sayHello? = ? function ?()
????????{
????????????alert( " welcome:?? " + this .name);
????????}
????};
???? var ?foo? = ? new ?Foo();
????foo.name? = ? " wh " ;
????foo.sayHello();
function?A(){???????????
????var?locate1?=?"1oh";????
????this.locate2?=?"2oh";????
????var?method1?=?function(){????
????????alert(locate1);????
????}????
????this.method2?=?function(){????
????????alert(this.locate2);????
????????method1();????
????}???????????????????????????????????
}?
A對象的locate1與method1是private級別的; locate2與method2是public級別的。??????????
????var?locate1?=?"1oh";????
????this.locate2?=?"2oh";????
????var?method1?=?function(){????
????????alert(locate1);????
????}????
????this.method2?=?function(){????
????????alert(this.locate2);????
????????method1();????
????}???????????????????????????????????
}?
posted on 2007-03-07 17:31 freebird 閱讀(298) 評論(0) 編輯 收藏 所屬分類: javascript