6)靜態方法和prototype(難)
例 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);html
文章轉載自:https://blog.csdn.net/qq_44594249/article/details/100053745java