`

define subclass - js

    博客分类:
  • js
 
阅读更多

 

define subclass - js

 

------

define subclass

 

definition of subclass:

assume there are function A and B, if A.prototype.prototype = B.prototype, then A is subclass of B,

to complete the subclass, there are more things to do than extends prototype, see following steps,

 

steps to create subclass: 

* B.prototype extends from A.prototype

* B.prototype.constructor = B

* in B's constructor body

* use apply()/call() to invoke B's constructor with arguments,

* define new things for B,

* override A's method/property, if necessary,

* define things on new constructor

 

------

how create a prototype extends from another prototype

 

how to create sub prototype:

create a new function whose prototype is the super prototype, then the new function could be used as the sub prototype,

how to do:

* in ECMAScript5:

use Object.create(p),

* in ECMAScript3:

create an function, point its prototype to ther super prototype,

 

util function to create a sub prototype:

	/**
	  * create a sub prototype which extends from specified super prototype
	  * 
	  * @param: p 
			super prototype
	  * @return
			sub prototype
	  */
	function inherit(p) {
	    if (p == null) throw TypeError(); // p must be a non-null object
	    if (Object.create)                // If Object.create() is defined...
		return Object.create(p);      //    then just use it.
	    var t = typeof p;                 // Otherwise do some more type checking
	    if (t !== "object" && t !== "function") throw TypeError();
	    function f() {};                  // Define a dummy constructor function.
	    f.prototype = p;                  // Set its prototype property to p.
	    return new f();                   // Use f() to create an "heir" of p.
	}

 

 

------

abstract class / interface

 

abstract class & interface:

are class that include abstract method, in js abstract method could be a method that throw an exception,

 

implementation:

in the subclass, override the abstract method, thus get a class without abstract method,

 

------

e.g.

 

include:

 

* inherit_util.js

include some util methods for inherit,

* subclass_example_1.js

a example use inherit()/extend() directly,

* subclass_example_2.js

a example use defineSubclass(),

* subclass_example_3.js

a example use Function.prototype.extend(),

 

 

 

inherit_util.js:

/**
 * util function copy enumerable property of p to object o
 * 
 * @param o
 * @param p
 * @returns
 */
function extend(o, p) {
	for (prop in p) { // For all props in p.
		o[prop] = p[prop]; // Add the property to o.
	}
	return o;
}

/**
 * create a prototype which inherit from specified prototype
 * 
 * @param p
 *            super prototype
 * @returns sub prototype
 */
function inherit(p) {
	if (p == null)
		throw TypeError(); // p must be a non-null object
	if (Object.create) // If Object.create() is defined...
		return Object.create(p); // then just use it.
	var t = typeof p; // Otherwise do some more type checking
	if (t !== "object" && t !== "function")
		throw TypeError();
	// Define a dummy constructor function.
	function f() {
	}

	f.prototype = p; // Set its prototype property to p.
	return new f(); // Use f() to create an "heir" of p.
}

/**
 * util function to define subclass, extends from specified superclass, and add
 * new methods/properties to prototype/constructor, this function depends on
 * extend() & inherit(),
 * 
 * @param superclass
 *            Constructor of the superclass
 * @param constructor
 *            The constructor for the new subclass
 * @param methods
 *            Instance methods to copied to prototype, the param should be an
 *            object contain methods/properties
 * @param statics
 *            Class properties to copied to constructor, the param should be a
 *            object contain methods/properties,
 * @return the new constructor
 */
function defineSubclass(superclass, constructor, methods, statics) {
	// inherit from super prototype
	constructor.prototype = inherit(superclass.prototype);
	// set the constructor property of the new prototype
	constructor.prototype.constructor = constructor;
	// copy instance properties/methods to copied to prototype
	if (methods)
		extend(constructor.prototype, methods);
	// copy class properties/methods to constructor
	if (statics)
		extend(constructor, statics);
	// Return the class
	return constructor;
}

/**
 * add the extend method to js Function, so that apply to all function of js,
 * 
 * @param constructor
 * @param methods
 * @param statics
 * @returns the new constructor
 */
Function.prototype.extend = function(constructor, methods, statics) {
	return defineSubclass(this, constructor, methods, statics);
};
/**
 * an abstract method, which throws error
 */
function abstractMethod() {
	throw new Error("method not implemented");
}

 

 

subclass_example_1.js:

// depends on inherit_util.js

// super class
function A() {
	this.pone = "A-pone";
	this.ptwo = "A-ptwo";
}
A.prototype.mone = function() {
	console.log("A-mone");
};
// super class has a abstract method
A.prototype.mtwo = abstractMethod;
A.prototype.mthree = function() {
	console.log("A-mthree");
};

// sub class
function B() {
	// invoke super constructor on sub object, so that to extends things defined
	// in super constructor,
	A.apply(this);
	// override super property from super constructor,
	this.pone = "B-pone";
}
// extends from super prototype
B.prototype = inherit(A.prototype);
// set the constructor property of sub prototype, after extends prototype,
B.prototype.constructor = B;
// implemet(override) abstract method of super class
B.prototype.mtwo = function() {
	console.log("B-mtwo");
};
// override method of super class
B.prototype.mthree = function() {
	console.log("B-mthree");
};

function test() {
	var b = new B();
	b.mone();
	b.mtwo();
	b.mthree();
	console.log(b.pone);
	console.log(b.ptwo);
}
test();

 

 

subclass_example_2.js:

// depends on inherit_util.js

// super class
function A() {
	this.pone = "A-pone";
	this.ptwo = "A-ptwo";
}
A.prototype.mone = function() {
	console.log("A-mone");
};
// super class has a abstract method
A.prototype.mtwo = abstractMethod;
A.prototype.mthree = function() {
	console.log("A-mthree");
};

// sub class
function B() {
	// invoke super constructor on sub object, so that to extends things defined
	// in super constructor,
	A.apply(this);
	// override super property from super constructor,
	this.pone = "B-pone";
}

// extends from super prototype
defineSubclass(A, B, {
	mtwo : function() {
		console.log("B-mtwo");
	},
	mfour : function() {
		console.log("B-mfour");
	}
}, {
	sfone : "staticFieldOne"
});

function test() {
	var b = new B();
	b.mone();
	b.mtwo();
	b.mthree();
	b.mfour();
	console.log(b.pone);
	console.log(b.ptwo);
	console.log(b.constructor.sfone);
}
test();

 

 

subclass_example_3.js:

 

// depends on inherit_util.js

// super class
function A() {
	this.pone = "A-pone";
	this.ptwo = "A-ptwo";
}
A.prototype.mone = function() {
	console.log("A-mone");
};
// super class has a abstract method
A.prototype.mtwo = abstractMethod;
A.prototype.mthree = function() {
	console.log("A-mthree");
};

// sub class
function B() {
	// invoke super constructor on sub object, so that to extends things defined
	// in super constructor,
	A.apply(this);
	// override super property from super constructor,
	this.pone = "B-pone";
}

// extends from super class
A.extend(B, {
	mtwo : function() {
		console.log("B-mtwo");
	},
	mfour : function() {
		console.log("B-mfour");
	}
}, {
	sfone : "staticFieldOne"
});

function test() {
	var b = new B();
	b.mone();
	b.mtwo();
	b.mthree();
	b.mfour();
	console.log(b.pone);
	console.log(b.ptwo);
	console.log(b.constructor.sfone);
}
test();
 

 

------


分享到:
评论

相关推荐

    define-class:轻松定义自定义类

    script src =" path/to/subclass.js " > </ script > < script src =" path/to/define-class.js " > </ script > < script > $ . defineClass ( 'my-class' , function ( pt ) { 'use ...

    Ext实现java的面向对象实例

    在Ext中,类实例属性可以通过类定义的配置项或者构造函数来设置,如`Ext.define('Person', {name: 'John'}`或`new Person({name: 'John'})`。 3. **类实例方法**:这些方法属于类的实例,每个实例都可以有自己的...

    避开10大常见坑:DeepSeekAPI集成中的错误处理与调试指南.pdf

    在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!

    前端分析-2023071100789

    前端分析-2023071100789

    基于kinect的3D人体建模C++完整代码.cpp

    基于kinect的3D人体建模C++完整代码.cpp

    搞机工具箱10.1.0.7z

    搞机工具箱10.1.0.7z

    GRU+informer时间序列预测(Python完整源码和数据)

    GRU+informer时间序列预测(Python完整源码和数据),python代码,pytorch架构,适合各种时间序列直接预测。 适合小白,注释清楚,都能看懂。功能如下: 代码基于数据集划分为训练集测试集。 1.多变量输入,单变量输出/可改多输出 2.多时间步预测,单时间步预测 3.评价指标:R方 RMSE MAE MAPE,对比图 4.数据从excel/csv文件中读取,直接替换即可。 5.结果保存到文本中,可以后续处理。 代码带数据,注释清晰,直接一键运行即可,适合新手小白。

    性价比革命:DeepSeekAPI成本仅为GPT-4的3%的技术揭秘.pdf

    在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!

    基于ANSYS LSDyna的DEM-SPH-FEM耦合模拟滑坡入水动态行为研究,基于ANSYS LSDyna的DEM-SPH-FEM耦合的滑坡入水模拟分析研究,基于ansys lsdyna的滑坡入水

    基于ANSYS LSDyna的DEM-SPH-FEM耦合模拟滑坡入水动态行为研究,基于ANSYS LSDyna的DEM-SPH-FEM耦合的滑坡入水模拟分析研究,基于ansys lsdyna的滑坡入水模拟dem-sph-fem耦合 ,基于ANSYS LSDyna; 滑坡入水模拟; DEM-SPH-FEM 耦合,基于DEM-SPH-FEM耦合的ANSYS LSDyna滑坡入水模拟

    auto_gptq-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

    auto_gptq-0.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

    复件 复件 建设工程可行性研究合同[示范文本].doc

    复件 复件 建设工程可行性研究合同[示范文本].doc

    13考试真题最近的t64.txt

    13考试真题最近的t64.txt

    Microsoft Visual C++ 2005 SP1 Redistributable PackageX86

    好用我已经解决报错问题

    嵌入式开发入门:用C语言点亮LED灯的全栈开发指南.pdf

    # 踏入C语言的奇妙编程世界 在编程的广阔宇宙中,C语言宛如一颗璀璨恒星,以其独特魅力与强大功能,始终占据着不可替代的地位。无论你是编程小白,还是有一定基础想进一步提升的开发者,C语言都值得深入探索。 C语言的高效性与可移植性令人瞩目。它能直接操控硬件,执行速度快,是系统软件、嵌入式开发的首选。同时,代码可在不同操作系统和硬件平台间轻松移植,极大节省开发成本。 学习C语言,能让你深入理解计算机底层原理,培养逻辑思维和问题解决能力。掌握C语言后,再学习其他编程语言也会事半功倍。 现在,让我们一起开启C语言学习之旅。这里有丰富教程、实用案例、详细代码解析,助你逐步掌握C语言核心知识和编程技巧。别再犹豫,加入我们,在C语言的海洋中尽情遨游,挖掘无限可能,为未来的编程之路打下坚实基础!

    auto_gptq-0.4.2-cp38-cp38-win_amd64.whl

    auto_gptq-0.4.2-cp38-cp38-win_amd64.whl

    自动立体库设计方案.pptx

    自动立体库设计方案.pptx

    手把手教你用C语言实现贪吃蛇游戏:从算法设计到图形渲染.pdf

    # 踏入C语言的奇妙编程世界 在编程的广阔宇宙中,C语言宛如一颗璀璨恒星,以其独特魅力与强大功能,始终占据着不可替代的地位。无论你是编程小白,还是有一定基础想进一步提升的开发者,C语言都值得深入探索。 C语言的高效性与可移植性令人瞩目。它能直接操控硬件,执行速度快,是系统软件、嵌入式开发的首选。同时,代码可在不同操作系统和硬件平台间轻松移植,极大节省开发成本。 学习C语言,能让你深入理解计算机底层原理,培养逻辑思维和问题解决能力。掌握C语言后,再学习其他编程语言也会事半功倍。 现在,让我们一起开启C语言学习之旅。这里有丰富教程、实用案例、详细代码解析,助你逐步掌握C语言核心知识和编程技巧。别再犹豫,加入我们,在C语言的海洋中尽情遨游,挖掘无限可能,为未来的编程之路打下坚实基础!

    性能对决:DeepSeek-V3与ChatGPTAPI在数学推理场景的基准测试.pdf

    在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!

    从零到一:手把手教你用Python调用DeepSeekAPI的完整指南.pdf

    在日常的工作和学习中,你是否常常为处理复杂的数据、生成高质量的文本或者进行精准的图像识别而烦恼?DeepSeek 或许就是你一直在寻找的解决方案!它以其高效、智能的特点,在各个行业都展现出了巨大的应用价值。然而,想要充分发挥 DeepSeek 的优势,掌握从入门到精通的知识和技能至关重要。本文将从实际应用的角度出发,为你详细介绍 DeepSeek 的基本原理、操作方法以及高级技巧。通过系统的学习,你将能够轻松地运用 DeepSeek 解决实际问题,提升工作效率和质量,让自己在职场和学术领域脱颖而出。现在,就让我们一起开启这场实用又高效的学习之旅吧!

    为什么你的switch总出bug?90%新手不知道的break语句隐藏规则.pdf

    # 踏入C语言的奇妙编程世界 在编程的广阔宇宙中,C语言宛如一颗璀璨恒星,以其独特魅力与强大功能,始终占据着不可替代的地位。无论你是编程小白,还是有一定基础想进一步提升的开发者,C语言都值得深入探索。 C语言的高效性与可移植性令人瞩目。它能直接操控硬件,执行速度快,是系统软件、嵌入式开发的首选。同时,代码可在不同操作系统和硬件平台间轻松移植,极大节省开发成本。 学习C语言,能让你深入理解计算机底层原理,培养逻辑思维和问题解决能力。掌握C语言后,再学习其他编程语言也会事半功倍。 现在,让我们一起开启C语言学习之旅。这里有丰富教程、实用案例、详细代码解析,助你逐步掌握C语言核心知识和编程技巧。别再犹豫,加入我们,在C语言的海洋中尽情遨游,挖掘无限可能,为未来的编程之路打下坚实基础!

Global site tag (gtag.js) - Google Analytics