到目前为止,我已经写完了面向对象完整的一个系列知识,前面基本属于理论,原理的理解,接下来,我们就用学到的知识来实战下吧.

看看理解原理和理论是否重要?例子从简单到复杂

一、单体(字面量)封装加减乘除

             var Oper = {
add : function( n1, n2 ){
return n1 + n2;
},
sbb : function( n1, n2 ){
return n1 - n2;
},
mul : function( n1, n2 ){
return n1 * n2;
},
div : function( n1, n2 ){
return n1 / n2;
},
};
console.log( Oper.add( 10, 20 ) ); //
console.log( Oper.sbb( 10, 20 ) ); //-10
console.log( Oper.mul( 10, 20 ) ); //
console.log( Oper.div( 10, 20 ) ); //0.5

二、构造函数方式

         function Oper( n1, n2 ){
this.num1 = n1 || 0;
this.num2 = n2 || 0;
this.setData = function( n1, n2 ){
this.num1 = n1;
this.num2 = n2;
};
this.add = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 + this.num2;
};
this.sbb = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 - this.num2;
};
this.mul = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 * this.num2;
};
this.div = function(){
this.setData( arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0 );
return this.num1 / this.num2;
};
}; console.log( new Oper( 10, 20 ).add() ); //
console.log( new Oper(100, 200).sbb( 10, 20 ) ); //-10
console.log( new Oper().mul( 10, 20 ) ); //
console.log( new Oper().div( 10, 20 ) ); //0.5

三、构造函数+原型对象(prototype)

         function Oper(n1, n2) {
this.num1 = n1 || 0;
this.num2 = n2 || 0;
};
Oper.prototype = {
constructor : Oper,
setData : function (n1, n2) {
this.num1 = n1;
this.num2 = n2;
},
add : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 + this.num2;
},
sbb : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 - this.num2;
},
mul : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 * this.num2;
},
div : function () {
this.setData(arguments[0] || this.num1 || 0, arguments[1] || this.num2 || 0);
return this.num1 / this.num2;
}
};
console.log(new Oper().add(10, 20)); //
console.log(new Oper( 100, 200 ).sbb()); //-100
console.log(new Oper().mul(10, 20)); //
console.log(new Oper().div(10, 20)); //0.5

四、寄生组合继承+简单工厂模式

         function Oper( n1, n2 ){
this.num1 = n1;
this.num2 = n2;
};
Oper.prototype.run = function(){}
function object( o ){
var G = function(){};
G.prototype = o;
return new G();
};
function inheritPrototype( subObj, superObj ){
var proObj = object( superObj.prototype );
proObj.constructor = subObj;
subObj.prototype = proObj;
} function OperAdd( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperAdd, Oper );
OperAdd.prototype.run = function(){
return this.num1 + this.num2;
} function OperSbb( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperSbb, Oper );
OperSbb.prototype.run = function(){
return this.num1 - this.num2;
} function OperMul( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperMul, Oper );
OperMul.prototype.run = function(){
return this.num1 * this.num2;
} function OperDiv( n1, n2 ){
Oper.call( this, n1, n2 );
}
inheritPrototype( OperDiv, Oper );
OperDiv.prototype.run = function(){
return this.num1 / this.num2;
} function OperFactory( oper, n1, n2 ){
switch( oper ) {
case '+':
return new OperAdd( n1, n2 ).run();
break;
case '-':
return new OperSbb( n1, n2 ).run();
break;
case '*':
return new OperMul( n1, n2 ).run();
break;
case '/':
return new OperDiv( n1, n2 ).run();
break;
}
} console.log( OperFactory( '+', 10, 20 ) ); //
console.log( OperFactory( '-', 10, 20 ) ); //-10
console.log( OperFactory( '*', 10, 20 ) ); //
console.log( OperFactory( '/', 10, 20 ) ); //0.5

这种方式,虽然增加了代码量, 如果这道题是实际运用,比如说后面还有很多种运算,两个数的乘方,立方,平方等等,

还有其他特殊处理等等,那么这种扩展性就非常强

[js高手之路]面向对象+设计模式+继承一步步改造简单的四则运算的更多相关文章

  1. [js高手之路]面向对象版本匀速运动框架

    这篇文章的效果,需要看过以下3篇文章: [js插件开发教程]一步步开发一个可以定制配置的隔行变色小插件 [js高手之路]匀速运动与实例实战(侧边栏,淡入淡出) [js高手之路]打造通用的匀速运动框架 ...

  2. [js高手之路]寄生组合式继承的优势

    在之前javascript面向对象系列的文章里面,我们已经探讨了组合继承和寄生继承,回顾下组合继承: function Person( uName ){ this.skills = [ 'php', ...

  3. [js高手之路]原型式继承与寄生式继承

    一.原型式继承本质其实就是个浅拷贝,以一个对象为模板复制出新的对象 function object( o ){ var G = function(){}; G.prototype = o; retur ...

  4. [js高手之路] javascript面向对象写法与应用

    一.什么是对象? 对象是n个属性和方法组成的集合,如js内置的document, Date, Regexp, Math等等 document就是有很多的属性和方法, 如:getElementById, ...

  5. [js高手之路]设计模式系列课程-发布者,订阅者重构购物车

    发布者订阅者模式,是一种很常见的模式,比如: 一.买卖房子 生活中的买房,卖房,中介就构成了一个发布订阅者模式,买房的人,一般需要的是房源,价格,使用面积等信息,他充当了订阅者的角色 中介拿到卖主的房 ...

  6. [js高手之路]设计模式系列课程-组合模式+寄生组合继承实战新闻列表

    所谓组合模式,就是把一堆结构分解出来,组成在一起,现实中很多这样的例子,如: 1.肯德基套餐就是一种组合模式, 比如鸡腿堡套餐,一般是是由一个鸡腿堡,一包薯条,一杯可乐等组成的 2.组装的台式机同理, ...

  7. [js高手之路]从原型链开始图解继承到组合继承的产生

    基于javascript原型链的层层递进查找规则,以及原型对象(prototype)的共享特性,实现继承是非常简单的事情 一.把父类的实例对象赋给子类的原型对象(prototype),可以实现继承 f ...

  8. [js高手之路] 设计模式系列课程 - jQuery的extend插件机制

    这里在之前的文章[js高手之路] 设计模式系列课程 - jQuery的链式调用与灵活的构造函数基础上增加一个extend浅拷贝,可以为对象方便的扩展属性和方法, jquery的插件扩展机制,大致就是这 ...

  9. [js高手之路]Node.js+jade抓取博客所有文章生成静态html文件

    这个周末,恶补了一下jade模板引擎,就为生成静态html文件,这篇文章需要知道jade以及看过我的上篇文章,我先给出他们的参考链接: [js高手之路]Node.js模板引擎教程-jade速学与实战1 ...

随机推荐

  1. Kintinuous 相关论文 Volume Fusion 详解

    近几个月研读了不少RGBD-SLAM的相关论文,Whelan的Volume Fusion系列文章的效果确实不错,而且开源代码Kintinuous结构清晰,易于编译和运行,故把一些学习时自己的理解和经验 ...

  2. nodejs抓取网络图片转换为base64编码的图片

    抓取网络图片需要加载http模块 //假定这是index.js文件 var http = require('http'); var url = 'http://p0.meituan.net/tuanp ...

  3. 关于sessionStorage的移动端兼容问题

    最近在开发移动端项目时,需要用到的本地存储的地方不少.都是一些只要记住当前打开窗口的用户数据就行,所以我选择用的sessionStorage.使用场景如下: A.html页面需要记录一条数据{a:1, ...

  4. U盘发现器

    U盘发现器 package com.lx.io; import java.io.File; import java.io.IOException; import java.util.ArrayList ...

  5. 华为OJ之放苹果

    题目描述: 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法.输入每个用例包含二个整数M和N.0<=m< ...

  6. 关于启动文件分析的(MDK-ARM) 【转】

    ;******************** (C) COPYRIGHT 2010 STMicroelectronics ******************** ;* File Name : star ...

  7. (转)sizeof

    数据类型的大小(即所占字节数)以及能够表示的数据范围是与编译器和硬件平台有关的."float.h"头文件(如vc6.0,在include目录下)通常定义了基本数据类型能够表示的数据 ...

  8. HDOJ2012-素数判定

    Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数.   I ...

  9. webIDE 第二篇博文

    这是我做webIDE过程中的第二篇博文,之所以隔了这么长时间没更,因为确实是没有啥进度啊,没什么可写的,现在虽然依然没啥进度,但中途遇到很多坑,这些坑还是有记录下来的必要的. 因个人水平问题,可能有的 ...

  10. Angular4.0从入门到实战打造在线竞拍网站学习笔记之三--依赖注入

    Angular4.0基础知识之组件 Angular4.0基础知识之路由 依赖注入(Dependency Injection) 正常情况下,我们写的代码应该是这样子的: let product = ne ...