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);
文章轉載自:https://blog.csdn.net/qq_44594249/article/details/100053745
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/156512.html
標籤:JavaScript
上一篇:一起學Vue之串列渲染
