js深入研究之扩展类,克隆对象,混合类(自定义的extend函数,clone函数,与augment函数)
1.类扩展
- /* EditInPlaceField类 */
- /* 扩展函数 */
- function extend(subClass, superClass) {
- var F = function() {};
- F.prototype = superClass.prototype;
- subClass.prototype = new F();
- subClass.prototype.constructor = subClass;
- subClass.superclass = superClass.prototype;
- if(superClass.prototype.constructor == Object.prototype.constructor) {
- superClass.prototype.constructor = superClass;
- }
- }
- function EditInPlaceField(id, parent, value) { // 构造函数
- this.id = id;
- this.value = value || 'default value';
- this.parentElement = parent;
- this.createElements(this.id);
- this.attachEvents();
- };
- EditInPlaceField.prototype = {
- createElements: function(id) {
- this.containerElement = document.createElement('div');
- this.parentElement.appendChild(this.containerElement);
- this.staticElement = document.createElement('span');
- this.containerElement.appendChild(this.staticElement);
- this.staticElement.innerHTML = this.value;
- this.fieldElement = document.createElement('input');
- this.fieldElement.type = 'text';
- this.fieldElement.value = this.value;
- this.containerElement.appendChild(this.fieldElement);
- this.saveButton = document.createElement('input');
- this.saveButton.type = 'button';
- this.saveButton.value = 'Save';
- this.containerElement.appendChild(this.saveButton);
- this.cancelButton = document.createElement('input');
- this.cancelButton.type = 'button';
- this.cancelButton.value = 'Cancel';
- this.containerElement.appendChild(this.cancelButton);
- this.convertToText();
- },
- attachEvents: function() {
- var that = this;
- addEvent(this.staticElement, 'click', function() { that.convertToEditable(); });
- addEvent(this.saveButton, 'click', function() { that.save(); });
- addEvent(this.cancelButton, 'click', function() { that.cancel(); });
- },
- convertToEditable: function() {
- this.staticElement.style.display = 'none';
- this.fieldElement.style.display = 'inline';
- this.saveButton.style.display = 'inline';
- this.cancelButton.style.display = 'inline';
- this.setValue(this.value);
- },
- save: function() {
- this.value = this.getValue();
- var that = this;
- var callback = {
- success: function() { that.convertToText(); },
- failure: function() { alert('Error saving value.'); }
- };
- ajaxRequest('GET', 'save.php?id=' + this.id + '&value=' + this.value, callback);
- },
- cancel: function() {
- this.convertToText();
- },
- convertToText: function() {
- this.fieldElement.style.display = 'none';
- this.saveButton.style.display = 'none';
- this.cancelButton.style.display = 'none';
- this.staticElement.style.display = 'inline';
- this.setValue(this.value);
- },
- setValue: function(value) {
- this.fieldElement.value = value;
- this.staticElement.innerHTML = value;
- },
- getValue: function() {
- return this.fieldElement.value;
- }
- };
- var titleClassical = new EditInPlaceField('titleClassical', $('doc'), 'Title Here');
- var currentTitleText = titleClassical.getValue();
- /* EditInPlaceArea类 */
- function EditInPlaceArea(id, parent, value) {
- EditInPlaceArea.superclass.constructor.call(this, id, parent, value);
- };
- extend(EditInPlaceArea, EditInPlaceField);
- // Override certain methods.
- EditInPlaceArea.prototype.createElements = function(id) {
- this.containerElement = document.createElement('div');
- this.parentElement.appendChild(this.containerElement);
- this.staticElement = document.createElement('p');
- this.containerElement.appendChild(this.staticElement);
- this.staticElement.innerHTML = this.value;
- this.fieldElement = document.createElement('textarea');
- this.fieldElement.value = this.value;
- this.containerElement.appendChild(this.fieldElement);
- this.saveButton = document.createElement('input');
- this.saveButton.type = 'button';
- this.saveButton.value = 'Save';
- this.containerElement.appendChild(this.saveButton);
- this.cancelButton = document.createElement('input');
- this.cancelButton.type = 'button';
- this.cancelButton.value = 'Cancel';
- this.containerElement.appendChild(this.cancelButton);
- this.convertToText();
- };
- EditInPlaceArea.prototype.convertToEditable = function() {
- this.staticElement.style.display = 'none';
- this.fieldElement.style.display = 'block';
- this.saveButton.style.display = 'inline';
- this.cancelButton.style.display = 'inline';
- this.setValue(this.value);
- };
- EditInPlaceArea.prototype.convertToText = function() {
- this.fieldElement.style.display = 'none';
- this.saveButton.style.display = 'none';
- this.cancelButton.style.display = 'none';
- this.staticElement.style.display = 'block';
- this.setValue(this.value);
- };
2.对象克隆
- /* EditInPlaceField对象*/
- /* 克隆函数 */
- function clone(object) {
- function F() {}
- F.prototype = object;
- return new F;
- }
- var EditInPlaceField = {
- configure: function(id, parent, value) {
- this.id = id;
- this.value = value || 'default value';
- this.parentElement = parent;
- this.createElements(this.id);
- this.attachEvents();
- },
- createElements: function(id) {
- this.containerElement = document.createElement('div');
- this.parentElement.appendChild(this.containerElement);
- this.staticElement = document.createElement('span');
- this.containerElement.appendChild(this.staticElement);
- this.staticElement.innerHTML = this.value;
- this.fieldElement = document.createElement('input');
- this.fieldElement.type = 'text';
- this.fieldElement.value = this.value;
- this.containerElement.appendChild(this.fieldElement);
- this.saveButton = document.createElement('input');
- this.saveButton.type = 'button';
- this.saveButton.value = 'Save';
- this.containerElement.appendChild(this.saveButton);
- this.cancelButton = document.createElement('input');
- this.cancelButton.type = 'button';
- this.cancelButton.value = 'Cancel';
- this.containerElement.appendChild(this.cancelButton);
- this.convertToText();
- },
- attachEvents: function() {
- var that = this;
- addEvent(this.staticElement, 'click', function() { that.convertToEditable(); });
- addEvent(this.saveButton, 'click', function() { that.save(); });
- addEvent(this.cancelButton, 'click', function() { that.cancel(); });
- },
- convertToEditable: function() {
- this.staticElement.style.display = 'none';
- this.fieldElement.style.display = 'inline';
- this.saveButton.style.display = 'inline';
- this.cancelButton.style.display = 'inline';
- this.setValue(this.value);
- },
- save: function() {
- this.value = this.getValue();
- var that = this;
- var callback = {
- success: function() { that.convertToText(); },
- failure: function() { alert('Error saving value.'); }
- };
- ajaxRequest('GET', 'save.php?id=' + this.id + '&value=' + this.value, callback);
- },
- cancel: function() {
- this.convertToText();
- },
- convertToText: function() {
- this.fieldElement.style.display = 'none';
- this.saveButton.style.display = 'none';
- this.cancelButton.style.display = 'none';
- this.staticElement.style.display = 'inline';
- this.setValue(this.value);
- },
- setValue: function(value) {
- this.fieldElement.value = value;
- this.staticElement.innerHTML = value;
- },
- getValue: function() {
- return this.fieldElement.value;
- }
- };
- var titlePrototypal = clone(EditInPlaceField);
- titlePrototypal.configure(' titlePrototypal ', $('doc'), 'Title Here');
- var currentTitleText = titlePrototypal.getValue();
- /* EditInPlaceArea对象*/
- var EditInPlaceArea = clone(EditInPlaceField);
- // Override certain methods.
- EditInPlaceArea.createElements = function(id) {
- this.containerElement = document.createElement('div');
- this.parentElement.appendChild(this.containerElement);
- this.staticElement = document.createElement('p');
- this.containerElement.appendChild(this.staticElement);
- this.staticElement.innerHTML = this.value;
- this.fieldElement = document.createElement('textarea');
- this.fieldElement.value = this.value;
- this.containerElement.appendChild(this.fieldElement);
- this.saveButton = document.createElement('input');
- this.saveButton.type = 'button';
- this.saveButton.value = 'Save';
- this.containerElement.appendChild(this.saveButton);
- this.cancelButton = document.createElement('input');
- this.cancelButton.type = 'button';
- this.cancelButton.value = 'Cancel';
- this.containerElement.appendChild(this.cancelButton);
- this.convertToText();
- };
- EditInPlaceArea.convertToEditable = function() {
- this.staticElement.style.display = 'none';
- this.fieldElement.style.display = 'block';
- this.saveButton.style.display = 'inline';
- this.cancelButton.style.display = 'inline';
- this.setValue(this.value);
- };
- EditInPlaceArea.convertToText = function() {
- this.fieldElement.style.display = 'none';
- this.saveButton.style.display = 'none';
- this.cancelButton.style.display = 'none';
- this.staticElement.style.display = 'block';
- this.setValue(this.value);
- };
3.混合类
- /* 混合类 */
- /* 混合函数 */
- function augment(receivingClass, givingClass) {
- for(methodName in givingClass.prototype) {
- if(!receivingClass.prototype[methodName]) {
- receivingClass.prototype[methodName] = givingClass.prototype[methodName];
- }
- }
- }
- /* 改进的增加函数 */
- function augment(receivingClass, givingClass) {
- if(arguments[2]) { // Only give certain methods.
- for(var i = 2, len = arguments.length; i < len; i++) {
- receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]];
- }
- }
- else { // Give all methods.
- for(methodName in givingClass.prototype) {
- if(!receivingClass.prototype[methodName]) {
- receivingClass.prototype[methodName] = givingClass.prototype[methodName];
- }
- }
- }
- }
- var EditInPlaceMixin = function() {};
- EditInPlaceMixin.prototype = {
- createElements: function(id) {
- this.containerElement = document.createElement('div');
- this.parentElement.appendChild(this.containerElement);
- this.staticElement = document.createElement('span');
- this.containerElement.appendChild(this.staticElement);
- this.staticElement.innerHTML = this.value;
- this.fieldElement = document.createElement('input');
- this.fieldElement.type = 'text';
- this.fieldElement.value = this.value;
- this.containerElement.appendChild(this.fieldElement);
- this.saveButton = document.createElement('input');
- this.saveButton.type = 'button';
- this.saveButton.value = 'Save';
- this.containerElement.appendChild(this.saveButton);
- this.cancelButton = document.createElement('input');
- this.cancelButton.type = 'button';
- this.cancelButton.value = 'Cancel';
- this.containerElement.appendChild(this.cancelButton);
- this.convertToText();
- },
- attachEvents: function() {
- var that = this;
- addEvent(this.staticElement, 'click', function() { that.convertToEditable(); });
- addEvent(this.saveButton, 'click', function() { that.save(); });
- addEvent(this.cancelButton, 'click', function() { that.cancel(); });
- },
- convertToEditable: function() {
- this.staticElement.style.display = 'none';
- this.fieldElement.style.display = 'inline';
- this.saveButton.style.display = 'inline';
- this.cancelButton.style.display = 'inline';
- this.setValue(this.value);
- },
- save: function() {
- this.value = this.getValue();
- var that = this;
- var callback = {
- success: function() { that.convertToText(); },
- failure: function() { alert('Error saving value.'); }
- };
- ajaxRequest('GET', 'save.php?id=' + this.id + '&value=' + this.value, callback);
- },
- cancel: function() {
- this.convertToText();
- },
- convertToText: function() {
- this.fieldElement.style.display = 'none';
- this.saveButton.style.display = 'none';
- this.cancelButton.style.display = 'none';
- this.staticElement.style.display = 'inline';
- this.setValue(this.value);
- },
- setValue: function(value) {
- this.fieldElement.value = value;
- this.staticElement.innerHTML = value;
- },
- getValue: function() {
- return this.fieldElement.value;
- }
- };
- /* EditInPlaceField class. */
- function EditInPlaceField(id, parent, value) {
- this.id = id;
- this.value = value || 'default value';
- this.parentElement = parent;
- this.createElements(this.id);
- this.attachEvents();
- };
- augment(EditInPlaceField, EditInPlaceMixin);
- /* EditInPlaceArea class. */
- function EditInPlaceArea(id, parent, value) {
- this.id = id;
- this.value = value || 'default value';
- this.parentElement = parent;
- this.createElements(this.id);
- this.attachEvents();
- };
- // Add certain methods so that augment won't include them.
- EditInPlaceArea.prototype.createElements = function(id) {
- this.containerElement = document.createElement('div');
- this.parentElement.appendChild(this.containerElement);
- this.staticElement = document.createElement('p');
- this.containerElement.appendChild(this.staticElement);
- this.staticElement.innerHTML = this.value;
- this.fieldElement = document.createElement('textarea');
- this.fieldElement.value = this.value;
- this.containerElement.appendChild(this.fieldElement);
- this.saveButton = document.createElement('input');
- this.saveButton.type = 'button';
- this.saveButton.value = 'Save';
- this.containerElement.appendChild(this.saveButton);
- this.cancelButton = document.createElement('input');
- this.cancelButton.type = 'button';
- this.cancelButton.value = 'Cancel';
- this.containerElement.appendChild(this.cancelButton);
- this.convertToText();
- };
- EditInPlaceArea.prototype.convertToEditable = function() {
- this.staticElement.style.display = 'none';
- this.fieldElement.style.display = 'block';
- this.saveButton.style.display = 'inline';
- this.cancelButton.style.display = 'inline';
- this.setValue(this.value);
- };
- EditInPlaceArea.prototype.convertToText = function() {
- this.fieldElement.style.display = 'none';
- this.saveButton.style.display = 'none';
- this.cancelButton.style.display = 'none';
- this.staticElement.style.display = 'block';
- this.setValue(this.value);
- };
- augment(EditInPlaceArea, EditInPlaceMixin);
点评:
js分为类和对象、函数。
其中又包含多种形式,属性,数组属性,函数,私有函数,公有函数,静态函数。
小的基础方法,可以有大的用途,比如extend方法,clone方法,还有augment方法。
js深入研究之扩展类,克隆对象,混合类(自定义的extend函数,clone函数,与augment函数)的更多相关文章
- 0604-面向对象、类与对象、类、static、构造方法/析构方法
一.面向对象 1.面向过程:一个人分步骤完成某个事情 2.面向对象:某件事情拆分为多个任务,由每个对象独立完成,最后调用整合为一个完整的项目 3.三要素:继承.封装.多态. 封装:私有化属性 提供公共 ...
- c++中的类的对象与类的指针
以上内容来自:http://wenku.baidu.com/link?url=haeRBhswlEcqddk48uW8YVMsdFNWsllimn_dzUYchb6G9NdT4pqgluCpnLQId ...
- 【学习笔记】【oc】类和对象及类的三大基本特征
1.类和对象 类是抽象化,对象是具体化. (1)定义类: 分为两个步骤,类的声明:定义类的成员变量和方法:@interface 用于声明定义类的接口部分,@end表面定义结束:. 成员变量的定义:{} ...
- python的类和对象(类的静态字段)
转自:http://www.cnblogs.com/Eva-J/p/5044411.html 什么是静态字段 在开始之前,先上图,解释一下什么是类的静态字段(我有的时候会叫它类的静态变量,总之说的都是 ...
- python: 面向对象:类和对象调用类中的变量和方法
一. 面向对象初识 我们在生活中做事都是面向过程的,前面实现一些基本逻辑功能代码也是用面向过程的语句实现的,后来学了函数,把这些功能又装到了函数里.但用面向过程的方法去写程序,只能实现一个功能,我们要 ...
- C#类,对象,类成员简介
本节内容 1.类(class)是现实世界事物的模型 2.类与对象的关系,什么时候叫“对象”什么时候叫“实例” 3.引用变量与实例的关系 4.类的三大成员: ①属性(Property): ②方法(Met ...
- C++类的对象和类的指针的区别
#include <iostream> #include <string> using namespace std; class Student { public: stati ...
- php函数、类和对象以及类的封装、继承、类的静态方法、静态属性
1.函数 php内置函数可以直接使用,如果没有安装php扩展即可 自定义函数 //函数function 函数名 function dump($var = null){ //支出默认参数 ...
- python__高级 : 动态添加 对象属性, 类属性, 对象实例方法, 类静态方法, 类方法
给对象添加实例属性,可以直接这样 t.age = 18 ( 假设 t = Test() ) 给类添加类属性 , 也可以直接这样 Test.age = 18 那给对象添加实例方法,可以在类外面 ...
随机推荐
- C++中使用stringstream进行类型转换操作
stringstream包括istringstream和ostringstream,提供读写string的功能,使用时需包含stream文件.4个操作:1. stringstream strm; 创建 ...
- Button 对象
<html> <form> <input type="button" value="提交" accesskey="b&q ...
- CSS常用选择器
关于CSS常用选择器: 1.ID选择器 关于ID选择器具有唯一性,在文档流中,ID是唯一的,在低版本的浏览器中,允许出现不适唯一ID的情况,而在高版本的浏览器中,出现ID不唯一的情况浏览器会出现的报错 ...
- 单源最短路径(dijkstra算法)php实现
做一个医学项目,当中在病例评分时会用到单源最短路径的算法.单源最短路径的dijkstra算法的思路例如以下: 如果存在一条从i到j的最短路径(Vi.....Vk,Vj),Vk是Vj前面的一顶点.那么( ...
- android4.4 settings 中控制卡1 卡2都振动
在package/app/Settings/src/com/android/settings/SoundSettings.java
- UDP C/S编程
UDP C/S编程的步骤如下图所示与TCP C/S通信的区别在于:服务端没有设置监听和等待连接的过程.客户端没有连接服务端的过程.基于UDP的通信时不可靠地,面向无连接的,发送的数据无法确切知道对方收 ...
- Spring简单的小例子SpringDemo,用于初略理解什么是Spring以及JavaBean的一些概念
一.开发前的准备 两个开发包spring-framework-3.1.1.RELEASE-with-docs.zip和commons-logging-1.2-bin.zip,将它们解压,然后把Spri ...
- js判断值是否为数字
js判断是否是数字 第一种方法 isNaN isNaN 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). NaN 即 Not a Number isNaN(numValu ...
- OC基础 NSDate
OC基础 NSDate #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @auto ...
- iOS项目名称、版本号与屏幕分辨率
iOS的版本号,一个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击“Summary”后看到. Version在plist文件中的key是“CFBundle ...