javascript當(dāng)中靜態(tài)方法和prototype用法

靜態(tài)方法和prototype(難)
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。



例 3.6.1

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
    /*note that 馬克-to-win: static variable's value has nothing to do with instance's variable's value.instance 名稱 can not 直接access static member like in java.
This is different from Java,比如下面例子中,Student.number=2,但是d1.number就為undefined.This is different from Java,但在實(shí)例方法中(比如d1.info)可以訪問Student.number。這是和java中一樣的?;蛘哒ffunction外或任何地方都可以訪問Student.number。反過來,d1.age也可以在靜態(tài)方法中訪問,就像在function外一樣,任何地方都能訪問d1.age。 String.prototype.abcd,這是給所有的實(shí)例加屬性而不是靜態(tài)屬性。*/
    function Student(number, agev)
    {
        this.age = agev;
        /*static variable's value can not be accessed by instance */
        Student.number = number;
        /*lb is local variable, but not a member variable because it is not modified by this. from outside it can not be accessed. refer to noblockScope.html */
        var lb = 0;
    }
    var d1 = new Student(1, 3);
    document.writeln("this的age屬性為means window.age" + this.age + "<br>");
    document.writeln("d1的age屬性為" + d1.age + "<br>");
    document.writeln("d1的number屬性為" + d1.number + "<br>");
    document.writeln("通過Student訪問靜態(tài)number屬性為" + Student.number + "<br>");
    document.writeln("d1的lb屬性為" + d1.lb + "<br><hr>");
    d1.qixy = "abc";/*以隨意為實(shí)例加屬性或方法*/
    document.writeln("可以隨意為實(shí)例加屬性或方法see following,d1的qixy屬性為" + d1.qixy + "<br><hr>");
    document.writeln("是否有靜態(tài)變量qixy" + Student.qixy + "<br><hr>");
    d1.info = function()/*此方法僅為d1對(duì)象所用*/
    {
        document.writeln("對(duì)象的qixy屬性:" + this.qixy);

        document.writeln("對(duì)象的age屬性:" + this.age);
        /*下列話是合法的, 因?yàn)椴皇莟his.number, 而是Student.number*/
        document.writeln("static method is " + Student.number);
    };
    Student.prototype.infop = function()/*此方法可以為所有Student對(duì)象所用*/
    {
        document.writeln("對(duì)象的qixy屬性p:" + this.qixy);
        document.writeln("對(duì)象的age屬性p:" + this.age);
        /*下列話是合法的, 因?yàn)椴皇莟his.number, 而是Student.number*/
        document.writeln("static method is " + Student.number);
    };
    Student.staticMethod = function()
    {
        /*下列話是合法的, 因?yàn)槭莇1.age,就像在function外一樣,任何地方都能訪問d1.age*/
        document.writeln("d1的age屬性為" + d1.age + "<br>");
        document.writeln("static method is " + Student.number);
    };
    d1.info();
    Student.staticMethod();
    var d2 = new Student(2, 4);
    /*the following statement can not be added, otherwise, it report error. because info is d1's method.
this is the beneit of prototype.prototype 能給類添加instance方法*/
    //    d2.info();
    d2.infop();
    d1.infop();
    document.writeln("d1的age屬性為" + d1.age + "<br>");
    document.writeln("d1的number屬性為" + d1.number + "<br>");
    document.writeln("d2的age屬性為" + d2.age + "<br>");
    document.writeln("d2的number屬性為" + d2.number + "<br>");
    document.writeln("通過Student訪問靜態(tài)number屬性為" + Student.number + "<br>");
    Student.number = 3;
    document.writeln("通過Student訪問靜態(tài)number屬性為" + Student.number + "<br>");
    Student.number1 = 31;
    document.writeln("通過Student訪問靜態(tài)number1屬性為" + Student.number1 + "<br>");
/*馬克-to-win: abc是靜態(tài)屬性。 只能通過String.abc這樣靜態(tài)的屬性來訪問。通過以下的as.abc這樣的
實(shí)例屬性是訪問不著的。用以下的String.prototype.abcd,這是給所有的實(shí)例加屬性而不是靜態(tài)屬性,用as.abcd這樣的實(shí)例屬性就能訪問著了*/
    /*When the Global object is created, it always has at least the following properties:
       Object object       Function object       Array object       String object
       Boolean object       Number object       Date object       Math object
       Value properties
   */
    String.abc = "qixy";
    document.writeln("通過String訪問靜態(tài)abc屬性為" + String.abc + "<br>");
    var as="aString"
    document.writeln("as.abc is " +as.abc  + "<br>");
    String.prototype.abcd="qixyqixy";
    document.writeln("as.abcd is " +as.abcd  + "<br>");
    /*a property can be nonexist, it is still can be printed out.*/
    document.writeln("d1的noexist屬性為" + d1.noexist + "<br><hr>");
    /* p3 does not exists, error is generated.so program will stop here. */
    document.writeln("p3的lb屬性為" + p3.lb + "<br><hr>");
    document.writeln("d1的noexist屬性為" + d1.noexist + "<br><hr>");
</script>
here is body which is  after the tag of script




輸出結(jié)果:

this的age屬性為means window.ageundefined
d1的age屬性為3
d1的number屬性為undefined
通過Student訪問靜態(tài)number屬性為1
d1的lb屬性為undefined
可以隨意為實(shí)例加屬性或方法see following,d1的qixy屬性為abc
是否有靜態(tài)變量qixyundefined
對(duì)象的qixy屬性:abc 對(duì)象的age屬性:3 static method is 1 d1的age屬性為3
static method is 1 對(duì)象的qixy屬性p:undefined 對(duì)象的age屬性p:4 static method is 2 對(duì)象的qixy屬性p:abc 對(duì)象的age屬性p:3 static method is 2 d1的age屬性為3
d1的number屬性為undefined
d2的age屬性為4
d2的number屬性為undefined
通過Student訪問靜態(tài)number屬性為2
通過Student訪問靜態(tài)number屬性為3
通過Student訪問靜態(tài)number1屬性為31
通過String訪問靜態(tài)abc屬性為qixy
as.abc is undefined
as.abcd is qixyqixy
d1的noexist屬性為undefined
here is body which is after the tag of script