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

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

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

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

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

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

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

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

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

// 定義在某個具體對象(實例)上的方法是實例方法
function ClassA() { //定義構造函數(shù)
};
var instance = new ClassA(); //新建一個實例
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代碼運行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼,可獲得如下運行結(jié)果:

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

希望本文所述對大家JavaScript程序設計有所幫助。

相關文章

最新評論