亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript基礎(chǔ)之靜態(tài)方法和實(shí)例方法分析

 更新時間:2018年12月26日 08:57:59   作者:techbrood  
這篇文章主要介紹了JavaScript基礎(chǔ)之靜態(tài)方法和實(shí)例方法,簡單分析了javascript靜態(tài)方法及實(shí)例方法的定義、使用相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了JavaScript靜態(tài)方法和實(shí)例方法。分享給大家供大家參考,具體如下:

直接定義在構(gòu)造函數(shù)上的方法和屬性是靜態(tài)的,  定義在構(gòu)造函數(shù)的原型和實(shí)例上的方法和屬性是非靜態(tài)的

/* -- 靜態(tài)方法 -- */
function ClassA() { //定義構(gòu)造函數(shù)
};
ClassA.func = function() { //在構(gòu)造函數(shù)上添加一個屬性(因?yàn)楹瘮?shù)也是對象)
  console.log("This is a static method");
}
var instance = new ClassA(); //新建一個實(shí)例
ClassA.func(); //This is a static method
instance.func(); //Error:instance.func is not a function

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼,可獲得如下運(yùn)行結(jié)果:

/* --- 實(shí)例方法 -- */
function ClassA() { //定義構(gòu)造函數(shù)
};
ClassA.prototype.func = function() { //在構(gòu)造函數(shù)的原型上添加方法
  console.log("This is an instance method.");
}
var instance = new ClassA(); //新建一個實(shí)例
ClassA.func(); // Error:ClassA.func is not a function
instance.func(); //This is an instance method.

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼,可獲得如下運(yùn)行結(jié)果:

// 定義在某個具體對象(實(shí)例)上的方法是實(shí)例方法
function ClassA() { //定義構(gòu)造函數(shù)
};
var instance = new ClassA(); //新建一個實(shí)例
instance.func = function() {
    console.log("This is an instance method.")
  }
  // ClassA.func(); // Error:ClassA.func is not a function
instance.func(); //This is an instance method.

使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼,可獲得如下運(yùn)行結(jié)果:

更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論