写一个算法,有时候可以用简单的方法就可以写出来,但是只能针对特定的环境,如果要能够适应不同的环境,就需要对算法进行优化,在优化的过程中,你会觉得非常神奇,下面来看一个简单的四则运算的算法编写方式:

1.简单粗暴的实现:直接创建一个对象,在对象上直接挂载加减乘除方法

  <script>
var per = {
add: function(n1, n2) {
return n1 + n2;
},
sbb: function(n1, n2) {
return n1 - n2;
},
multi: function(n1, n2) {
return n1 * n2;
},
div: function(n1, n2) {
return n1 / n2;
}, }
console.log(per.add(10, 20));
console.log(per.sbb(10, 20));
console.log(per.multi(10, 20));
console.log(per.div(10, 20));
</script>

运行结果:

2.采用构造函数的方式,把方法加减乘除方法写在构造函数中

 <script>
function OPP(n1, n2) {
this.num1 = n1 || 0; // 当传入参数n1时,设置this.num1 = n1,否则设置为0;
this.num2 = n2 || 0; // 当传入参数n2时,设置this.num2 = n2,否则设置为0;
this.setdata = function(n1, n2) {
this.num1 = n1 || 0; // 当传入参数n1时,设置this.num1 = n1,否则设置为0;
this.num2 = n2 || 0; // 当传入参数n2时,设置this.num2 = n2,否则设置为0;
};
// 函数的运行,首先设置相关元素的属性值,然后再进行调用
this.add = function() {
// 当add()函数传入参数时,那么设置参数就使用传入的参数arguments[0]/arguments[1],否则使用原先的构造函数的参数this.num1/this.num2
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 + this.num2;
};
this.sbb = function() {
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 - this.num2;
};
this.multi = function() {
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 * this.num2;
};
this.div = function() {
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 / this.num2;
}
}
console.log(new OPP(10, 20).add()); //
console.log(new OPP().add(10, 20)); //
console.log(new OPP(100, 200).add(10, 20)); //
console.log(new OPP(10, 20).sbb()); //-10
console.log(new OPP().sbb(10, 20)); //-10
console.log(new OPP(100, 200).sbb(10, 20)); //-10
console.log(new OPP(10, 20).multi()); //
console.log(new OPP().multi(10, 20)); //
console.log(new OPP(10, 20).div()); //0.5
console.log(new OPP().div(10, 20)); //0.5
</script>

运行结果:

3. 采用构造函数的原型对象的方式,即将调用函数挂载到了构造函数的原型对象上,当调用函数时,是通过原型链进行调用的,而上一个没有涉及到原型链的问题,这是与上一种方式的本质区别

 <script>
function OPP(n1, n2) {
this.num1 = n1 || 0; // 当传入参数n1时,设置this.num1 = n1,否则设置为0;
this.num2 = n2 || 0; // 当传入参数n2时,设置this.num2 = n2,否则设置为0;
}
OPP.prototype = {
constructor: OPP,
setdata: function(n1, n2) {
this.num1 = n1 || 0;
this.num2 = n2 || 0;
},
add: function() {
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 + this.num2;
},
sbb: function() {
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 - this.num2;
},
multi: function() {
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 * this.num2;
},
div: function() {
this.setdata(arguments[0] || this.num1, arguments[1] || this.num2);
return this.num1 / this.num2;
},
}; console.log(new OPP(10, 20).add()); //
console.log(new OPP().add(10, 20)); //
console.log(new OPP(100, 200).add(10, 20)); //
console.log(new OPP(10, 20).sbb()); //-10
console.log(new OPP().sbb(10, 20)); //-10
console.log(new OPP(100, 200).sbb(10, 20)); //-10
console.log(new OPP(10, 20).multi()); //
console.log(new OPP().multi(10, 20)); //
console.log(new OPP(10, 20).div()); //0.5
console.log(new OPP().div(10, 20)); //0.5
</script>

运行结果:

4. 使用继承的方式实现:

 <script>
function OPP(n1, n2) {
this.num1 = n1 || 0; // 当传入参数n1时,设置this.num1 = n1,否则设置为0;
this.num2 = n2 || 0; // 当传入参数n2时,设置this.num2 = n2,否则设置为0;
};
OPP.prototype.run = function() {
throw new Error('原型链中没有该方法,请从写该方法才能调用!');
}; function object(o) {
var G = function() {};
G.prototype = o;
return new G();
}; function inheritPrototype(subObj, superObj) {
//调用中间函数:object,实现把子类的原型对象指向中间函数G的实例,而G的实例指向想父类的原型对象,
// 同时把实例的constructor 属性指向子类;相当于在原型链中增加了一个实例,而实例作为子类的原型对象,这样子类就可以通过原型链实现对父类的继承了
var proObj = object(superObj.prototype);
// 调用object()函数的意义,基本上就是实现以下注释的功能
// var G = function() {};
// G.prototype = superObj.prototype;
// var proObj = new G();
proObj.constructor = subObj;
subObj.prototype = proObj;
}; function add(n1, n2) {
OPP.call(this, n1, n2);
};
inheritPrototype(add, OPP);
add.prototype.run = function() {
return this.num1 + this.num2;
}; function sbb(n1, n2) {
OPP.call(this, n1, n2);
};
inheritPrototype(sbb, OPP);
sbb.prototype.run = function() {
return this.num1 - this.num2;
}; function multi(n1, n2) {
OPP.call(this, n1, n2);
};
inheritPrototype(multi, OPP);
multi.prototype.run = function() {
return this.num1 * this.num2;
}; function div(n1, n2) {
OPP.call(this, n1, n2);
};
inheritPrototype(div, OPP);
div.prototype.run = function() {
return this.num1 / this.num2;
}; var huanying2015 = function(n1, n2, oper) {
switch (oper) {
case '+':
return new add(n1, n2).run();
break;
case '-':
return new sbb(n1, n2).run();
break;
case '*':
return new multi(n1, n2).run();
break;
case '/':
return new div(n1, n2).run();
break;
}
} console.log(huanying2015(100, 200, '+'));
console.log(huanying2015(100, 200, '-'));
console.log(huanying2015(100, 200, '*'));
console.log(huanying2015(100, 200, '/'));
</script>

运行结果:

js 编写一个神奇的四则运算的更多相关文章

  1. 用JS编写一个函数,返回数组中重复出现过的元素

    用JS编写一个函数,返回数组中重复出现过的元素,见下面的代码: , , , , , , , ]; var getRepeat = function (arr) { var obj = {}; , le ...

  2. JS高级---一个神奇的原型链

    一个神奇的原型链 <script> var divObj=document.getElementById("dv"); console.dir(divObj); //d ...

  3. 使用js编写一个简单的运动框架

    下班后,,没事捣鼓捣鼓个人的小爱好. 首先,说明我的这个运动框架(css所有属性)也是常见的框架一种,健壮性并不是太好,对于新手学习倒是挺好,,若是大神,老司机请拐弯. 上来,我们先定义一个区块,然后 ...

  4. 利用js编写一个简单的html表单验证,验证通过时提交数据(附源码)

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...

  5. 作业二:个人编程项目——编写一个能自动生成小学四则运算题目的程序

    1. 编写一个能自动生成小学四则运算题目的程序.(10分)   基本要求: 除了整数以外,还能支持真分数的四则运算. 对实现的功能进行描述,并且对实现结果要求截图.   本题发一篇随笔,内容包括: 题 ...

  6. Numeral.js 是一个用于格式化和数字四则运算的js 库

    1.Numeral.js 是一个用于格式化和数字四则运算的js 库. 2.支持多种语言,包含中文在内的17种语言. 在浏览器中引用js文件: <script src="numeral. ...

  7. 第二次作业:编写一个四则运算的"软件"

    - 题目: 请编写一个能自动生成小学四则运算题目的 “软件”. 让程序能接受用户输入答案,并判定对错. 最后给出总共 对/错 的数量. 需求分析: ●基本功能 ●实现100以内的加法 ●实现100以内 ...

  8. 利用ANTLR4实现一个简单的四则运算计算器

    利用ANTLR4实现一个简单的四则运算计算器 ANTLR4介绍 ANTLR能够自动地帮助你完成词法分析和语法分析的工作, 免去了手写去写词法分析器和语法分析器的麻烦 它是基于LL(k)的, 以递归下降 ...

  9. 为Node.js编写组件的几种方式

    本文主要备忘为Node.js编写组件的三种实现:纯js实现.v8 API实现(同步&异步).借助swig框架实现. 关键字:Node.js.C++.v8.swig.异步.回调. 简介 首先介绍 ...

随机推荐

  1. MATLAB在三维坐标中显示图片 并 使得图片部分透明

    要画一个光路图,本来可以用proe,但是鼠标不好用,有些操作也忘了,用MATLAB画了个.下面是用到的图片. 但是三维坐标中显示彩色图片的目标没有搞定,做了个灰度图,然后用仿射程序将彩色图片贴到了二维 ...

  2. Authentication required (packagist.phpcomposer.com) 账号密码到哪里获取?

    安装好composer后,执行composer install 报这个错 面对这个错有两种方法.1,他说你的composr 版本不够稳定,composer update 一下,或者 composer ...

  3. CentOS6系统防火墙开启、关闭、查看状态(转载)

    原文:https://blog.csdn.net/aaron_80726/article/details/79027760 注意:要进入到~目录 也就是家目录下才能查看防火墙 进入家目录:cd ~ 关 ...

  4. Redis 主从+哨兵+监控 (centos7.2 + redis 3.2.9 )

    环境准备: 192.168.0.2  redis01 主 192.168.0.3  redis02 从 192.168.0.4  redis03 从 Redis 主从搭建 一:下载并安装redis软件 ...

  5. TF-IDF算法原理

    原文:https://www.cnblogs.com/biyeymyhjob/archive/2012/07/17/2595249.html TF-IDF(term frequency–inverse ...

  6. sas 批量处理缺少缺失值

    DATA S.customer_grade;    SET S.customer_grade;    ARRAY NUM{*} _NUMERIC_;    DO I=1 TO DIM(NUM);   ...

  7. 使用命名管道的OVERLAPPED方式实现非阻塞模式编程 .

    命令管道是进程间通讯的一种常用方式,对于命令管道的介绍可以参考别的资料和书籍,这里推荐一个<VC++下命名管道编程的原理及实现>这篇博文,写得比较清楚.但是都是介绍了阻塞模式的编程,我这里 ...

  8. Android 打开高德地图、百度地图进行导航;打开第三方App去导航;

    抽成工具类了,复制下来就能直接用了,直接看代码吧: 高德地图Url Api: http://lbs.amap.com/api/amap-mobile/guide/android/navigation ...

  9. Solr——Windows下部署Solr6.6.0至Tomcat8.5.28(二)

    1,core理解 如果把solr理解为个数据库的话,那么core可以理解为数据库中的一张表,其实就是数据集合 在写本片文章之前看到网上很多教程都说需要找到solr.xml来配置core的信息,特此说明 ...

  10. es6基础(4)--字符串扩展

    //字符串扩展 { console.log('a','\u0061'); console.log('s','\u20BB7');//超过了0xffff console.log('s','\u{20BB ...