Among the static method and prototype javascript usage

6) static method and the prototype (hard)

Example 3.6.1

<head>
    <Meta HTTP-equiv = "Content-type" Content = "text / HTML; charset = UTF-. 8" />
</ head>
<Script>
    / * note that Mark -to-win:. static variable's value has nothing to do with instance's variable's value.instance name can not be directly Member static like in the Java Access
This IS Different from the Java, such as the example below, Student.number = 2, but d1.number it is undefined.This is different from Java, but in an instance method (such as d1.info) can access Student.number. This is the java and the like. Or function outside or anywhere can access Student.number. In turn, d1.age can also be accessed in a static method, the same as in the function outside, anywhere access d1.age. String.prototype.abcd, which is added to all instances of property rather than static properties. * /
    Function Student (Number, AGeV)
    {
        this.age = AGeV;
        / * static variable '
        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";/ * Add an instance in a random property or method * /
    document.writeln ( "free to add property or method as an example see following, qixy d1 attribute is" + d1.qixy + "<br> <hr
    document.writeln ( "whether static variable qixy" + + Student.qixy "<br> <HR>");
    d1.info = function () / * This method is used only for d1 object * /
    {
        document.writeln ( " qixy properties of the object: "+ this.qixy);

        document.writeln (" Age properties of the object: "+ this.age);
        / * the following words are legal, because it is not this.number, but Student.number * /
        document.writeln ( "static method iS" + Student.number);
    };
    Student.prototype.infop = function () / * this method can be used for all the Student object * /
    {
        document.writeln ( "p qixy object properties: "+ this.qixy);
        document.writeln (" p Age attribute object: "+ this.age);

Reprinted from the article: https://blog.csdn.net/qq_44594249/article/details/100053745

Guess you like

Origin www.cnblogs.com/renzhe111/p/12179179.html