javascript当中静态方法和prototype用法

静态方法和prototype(难)
马克- to-win:马克 java社区:防盗版实名手机尾号: 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,但在实例方法中(比如d1.info)可以访问Student.number。这是和java中一样的。或者说function外或任何地方都可以访问Student.number。反过来,d1.age也可以在静态方法中访问,就像在function外一样,任何地方都能访问d1.age。 String.prototype.abcd,这是给所有的实例加属性而不是静态属性。*/
    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访问静态number属性为" + Student.number + "<br>");
    document.writeln("d1的lb属性为" + d1.lb + "<br><hr>");
    d1.qixy = "abc";/*以随意为实例加属性或方法*/
    document.writeln("可以随意为实例加属性或方法see following,d1的qixy属性为" + d1.qixy + "<br><hr>");
    document.writeln("是否有静态变量qixy" + Student.qixy + "<br><hr>");
    d1.info = function()/*此方法仅为d1对象所用*/
    {
        document.writeln("对象的qixy属性:" + this.qixy);

        document.writeln("对象的age属性:" + this.age);
        /*下列话是合法的, 因为不是this.number, 而是Student.number*/
        document.writeln("static method is " + Student.number);
    };
    Student.prototype.infop = function()/*此方法可以为所有Student对象所用*/
    {
        document.writeln("对象的qixy属性p:" + this.qixy);
        document.writeln("对象的age属性p:" + this.age);
        /*下列话是合法的, 因为不是this.number, 而是Student.number*/
        document.writeln("static method is " + Student.number);
    };
    Student.staticMethod = function()
    {
        /*下列话是合法的, 因为是d1.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访问静态number属性为" + Student.number + "<br>");
    Student.number = 3;
    document.writeln("通过Student访问静态number属性为" + Student.number + "<br>");
    Student.number1 = 31;
    document.writeln("通过Student访问静态number1属性为" + Student.number1 + "<br>");
/*马克-to-win: abc是静态属性。 只能通过String.abc这样静态的属性来访问。通过以下的as.abc这样的
实例属性是访问不着的。用以下的String.prototype.abcd,这是给所有的实例加属性而不是静态属性,用as.abcd这样的实例属性就能访问着了*/
    /*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访问静态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




输出结果:

this的age属性为means window.ageundefined
d1的age属性为3
d1的number属性为undefined
通过Student访问静态number属性为1
d1的lb属性为undefined
可以随意为实例加属性或方法see following,d1的qixy属性为abc
是否有静态变量qixyundefined
对象的qixy属性:abc 对象的age属性:3 static method is 1 d1的age属性为3
static method is 1 对象的qixy属性p:undefined 对象的age属性p:4 static method is 2 对象的qixy属性p:abc 对象的age属性p:3 static method is 2 d1的age属性为3
d1的number属性为undefined
d2的age属性为4
d2的number属性为undefined
通过Student访问静态number属性为2
通过Student访问静态number属性为3
通过Student访问静态number1属性为31
通过String访问静态abc属性为qixy
as.abc is undefined
as.abcd is qixyqixy
d1的noexist属性为undefined
here is body which is after the tag of script