JS真是一门经典脚本语言,近日看一些JS的面向对象编程的文章,记录下来。
1.构建类:
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();
}
}