javascript Classical Inheritance
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javascript 物件導向繼承工具</title>
</head>
<body>
<script language="javascript">
// A simple helper that allows you to bind new functions to the
// prototype of an object
Function.prototype.method = function(name, func) {
this.prototype[name] = func;
return this;
};
// A (rather complex) function that allows you to gracefully inherit
// functions from other objects and be able to still call the 'parent'
// object's function
Function.method('inherits', function(parent) {
// Keep track of how many parent-levels deep we are
var depth = 0;
// Inherit the parent's methods
var proto = this.prototype = new parent();
// Create a new 'priveledged' function called 'uber', that when called
// executes any function that has been written over in the inheritance
this.method('uber', function uber(name) {
var func; // The function to be execute
var ret; // The return value of the function
var v = parent.prototype; // The parent's prototype
// If we're already within another 'uber' function
if (depth) {
// Go the necessary depth to find the orignal prototype
for ( var i = d; i > 0; i += 1 ) {
v = v.constructor.prototype;
}
// and get the function from that prototype
func = v[name];
// Otherwise, this is the first 'uber' call
} else {
// Get the function to execute from the prototype
func = proto[name];
// If the function was a part of this prototype
if ( func == this[name] ) {
// Go to the parent's prototype instead
func = v[name];
}
}
// Keep track of how 'deep' we are in the inheritance stack
depth += 1;
// Call the function to execute with all the arguments but the first
// (which holds the name of the function that we're executing)
ret = func.apply(this, Array.prototype.slice.apply(arguments, [1]));
// Reset the stack depth
depth -= 1;
// Return the return value of the execute function
return ret;
});
return this;
});
// A function for inheriting only a couple functions from a parent object,
// not every function using new parent()
Function.method('swiss', function(parent) {
// Go through all of the methods to inherit
for (var i = 1; i < arguments.length; i += 1) {
// The name of the method to import
var name = arguments[i];
// Import the method into this object's prototype
this.prototype[name] = parent.prototype[name];
}
return this;
});
// Create a new Person object constructor
function Person( name ) {
this.name = name;
}
// Add a new method to the Person object
Person.method( 'getName', function(){
return name;
});
// Create a new User object constructor
function User( name, password ) {
this.name = name;
this.password = password;
}
// Inherit all the methods from the Person object
User.inherits( Person );
// Add a new method to the User object
User.method( 'getPassword', function(){
return this.password;
});
// Overwrite the method created by the Person object,
// but call it again using the uber function
User.method( 'getName', function(){
return "My name is: " + this.uber('getName');
});
</script>
</body>
</html>
雖然自己初學 JavaScript 對這個util 工具指令碼還不是很理解 同時也發現JavaScript真的很強大,很靈活,相對於java它真的太靈活了,但我相信隨著我學習的深入及實踐的增多,我會慢慢的理解它的!
相關文章
- Javascript繼承機制分析+simple-inheritance原始碼分析JavaScript繼承原始碼
- case class inheritance
- [Relationships]Inheritance
- 說說Prototypal Inheritance
- Important Points for Inheritance in JavaImportJava
- Entity Framework Code-First(7):Inheritance StrategyFramework
- 一張圖看懂CSS cascade, specific, importance, inheritanceCSSImport
- PLC結構化文字(ST)——繼承(inheritance)繼承
- odoo 開發入門教程系列-繼承(Inheritance)Odoo繼承
- Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH)
- Inheritance with EF Code First: Part 2 – Table per Type (TPT)
- virtual inheritance 的妙用--------實現final類 (轉)
- Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC)
- PostgreSQL DBA(105) - pgAdmin(Don't do this:psql&inheritance)SQL
- 翻譯:《實用的Python程式設計》04_02_InheritancePython程式設計
- VB.Net中文教程(4) 類別繼承(Inheritance)關係 (轉)繼承
- Solidity知識點集———Address, Mapping, msg.sender, require, InheritanceSolidAPPUI
- Caused by: java.lang.IllegalStateException: Only single-level inheritance supported: XxxxServiceJavaException
- 《Effective C++》第三版-6. 繼承與物件導向設計(Inheritance and Object-Oriented Design)C++繼承物件Object
- JavaScript高階:JavaScript物件導向,JavaScript內建物件,JavaScript BOM,JavaScript封裝JavaScript物件封裝
- javaScript系列[06]-javaScript和thisJavaScript
- 【JavaScript學習】JavaScript物件建立JavaScript物件
- 【轉】eval()函式(javascript) - [javaScript]函式JavaScript
- [Javascript] How javascript read the property?JavaScript
- JavaScript -"this"JavaScript
- javascript ??JavaScript
- This in JavaScriptJavaScript
- “This” is For JavaScriptJavaScript
- javascript thisJavaScript
- JavaScriptJavaScript
- javaScript系列[05]-javaScript和JSONJavaScriptJSON
- 44 道 JavaScript 難題(JavaScript Puzzlers!)JavaScript
- 【轉向JavaScript系列】AST in Modern JavaScriptJavaScriptAST
- javascript,還是javascript的問題JavaScript
- JavaScript 教程之JavaScript常用框架簡介JavaScript框架
- 《深入理解JavaScript》——2.3 JavaScript有用嗎JavaScript
- 【JavaScript】--JavaScript總結一覽無餘JavaScript
- 【HTML、JAVASCRIPT、CSS】3、Javascript基本概念HTMLJavaScriptCSS