欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年12月26日 08:57:59   作者:techbrood  
這篇文章主要介紹了JavaScript基礎(chǔ)之靜態(tài)方法和實(shí)例方法,簡(jiǎn)單分析了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ù)上添加一個(gè)屬性(因?yàn)楹瘮?shù)也是對(duì)象)
  console.log("This is a static method");
}
var instance = new ClassA(); //新建一個(gè)實(shí)例
ClassA.func(); //This is a static method
instance.func(); //Error:instance.func is not a function

使用在線(xiàn)HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼,可獲得如下運(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(); //新建一個(gè)實(shí)例
ClassA.func(); // Error:ClassA.func is not a function
instance.func(); //This is an instance method.

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

// 定義在某個(gè)具體對(duì)象(實(shí)例)上的方法是實(shí)例方法
function ClassA() { //定義構(gòu)造函數(shù)
};
var instance = new ClassA(); //新建一個(gè)實(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.

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

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

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

相關(guān)文章

最新評(píng)論