js 编写一个神奇的四则运算

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

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>

运行结果:

js 编写一个神奇的四则运算

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>

运行结果:

js 编写一个神奇的四则运算

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>

运行结果:

js 编写一个神奇的四则运算

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 编写一个神奇的四则运算

上一篇:JVM源码---教你傻瓜式编译openjdk7(JAVA虚拟机爱好者必看)


下一篇:傻瓜式操作Nagios