转自:
http://ejohn.org/blog/simple-javascript-inheritance/
// 略有改动,以便自己阅读
// Inspired by base2 and Prototype
( function() {
var NAMESPACE = 'Class';
var initializing = false;
var fnTest = /xyz/.test( function() {
'xyz';
} ) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
var Class = function() {
};
// Check whether fn is a function
var isFunction = function(fn) {
return (typeof fn == 'function');
}
// Create a new Class that inherits from this class
Class.extend = function(obj) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var proto = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in obj) {
// Check if we're overwriting an existing function
var isOverwriting = isFunction(obj[name]) && isFunction(_super[name])
&& fnTest.test(obj[name]);
proto[name] = isOverwriting ? ( function(name, fn) {
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = isFunction(fn) ? fn.apply(this, arguments) : void (0);
this._super = tmp;
return ret;
};
} )(name, obj[name])
: obj[name];
}
// The dummy class constructor
function Klass() {
// All construction is actually done in the init method
if (!initializing && isFunction(this.init))
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Klass.prototype = proto;
// Enforce the constructor to be what we expect
Klass.constructor = Klass;
// And make this class extendable
Klass.extend = arguments.callee;
return Klass;
};
window[NAMESPACE] = Class;
})();
分享到:
相关推荐
John Resig在他的"Simple JavaScript Inheritance"中提出了一种优雅的方式来模拟类继承。他的方法灵感来源于base2和PrototypeJS库。 首先,我们来看代码的关键部分: ```javascript (function(){ var ...
更不用谈继承、多态了,为了模拟出一些其它面向对象编程语言的这些特性,有好多大牛写了给出了实现方式,看了John Resig的《Simple JavaScript Inheritance》这篇文章,深深被折服了,原来短短几十行javascript也...
下面将详细讲解JavaScript继承的几种常见方式,并结合`simple-inheritance`库的源码分析高级用法。 1. 原型链继承 这是最基础的继承方式,通过将子类的`prototype`指向父类的实例来实现。然而,这种方法存在问题,...
的Simple JavaScript Inheritance的原始想法和实现 对Q.Evented Class 的最初想法和实现 我只是提取了类代码并使其独立。 使用类事件 ClassEvent类将事件系统添加到基类中。 它提供了一种侦听和触发事件的机制。 ...
SIMP(Simple JavaScript Inheritance and Module Pattern)是一个旨在简化JavaScript编程的框架,它结合了面向对象的继承机制和模块化设计模式,以提高代码的可维护性和复用性。这个框架的核心目标是帮助开发者以更...
注:本章中的jClass的实现参考了Simple JavaScript Inheritance的做法。 首先让我们来回顾一下第一章中介绍的例子: function Person(name) { this.name = name; } Person.prototype = { getName: function() { ...
Understand the important concepts of OOP in JavaScript, such as scope, objects, inheritance, event delegation, and more Find out how the module design pattern is used in OOP in JavaScript Design and ...
A Simple Testing Ground Chapter 2. Grammar Section 2.1. Whitespace Section 2.2. Names Section 2.3. Numbers Section 2.4. Strings Section 2.5. Statements Section 2.6. Expressions Section 2.7. ...
7. **简单继承 (Simple Inheritance)**: 在JavaScript中,简单继承通常是指通过原型链实现的继承模式,即一个对象可以直接继承另一个对象的属性和方法。这可以通过`Object.create()`方法或通过修改原型链来实现。 ...
It's simple typing syntax enables building large, robust applications using object-oriented techniques and industry standard design principles. Packed with practical, real-world examples, this book ...
Gain an insight into core and advanced TypeScript language features including inheritance, generics, asynchronous programming techniques, promises, decorators and more Integrate your existing ...
Work through building a simple application from scratch with clear, step-by-step examples Leap ahead of the learning-curve with best practice examples from an experienced Flight developer In Detail ...
This simple guide is packed with practical examples of solutions to common problems. Each chapter includes exercises and the possibility for you to test your progress by answering questions. ...