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();
或者如下:
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级别的。          
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章