<script type="text/javascript">
function add(x,y) { return x + y; }
function subtract(x,y) { return x - y; }
function multiply(x,y) { return x * y; }
function divide(x,y) { return x / y; }function operate(operator, operand1, operand2)
{
return operator (operand1, operand2);
}var i = operate(add, operate(add, 2, 3), operate(multiply, 4, 5));var operators = {
add: function(x,y) { return x+y; },
subtract: function(x,y) { return x-y; },
multiply: function(x,y) { return x*y; },
divide: function(x,y) { return x/y; },
pow: Math.pow};function operate2(op_name, operand1, operand2)
{
if (typeof operators[op_name] == "function")
return operators[op_name](operand1, operand2);
else throw "unknown operator";
}var j = operate2("add", "hello", operate2("add", " ", "world"))
var k = operate2("pow", 10, 2)document.write(j);
document.write('<br>');
document.write(k);
</script>
我想问一下关于黄色字的问题是,为什么POW的TYPEOF是function?这里的function意思是指在operators 对象中存在的意思吗还有一个问题是蓝色字~math.pow是怎么分底数和次幂的?转的值是(10底数,2次幂),这个是内部定义的吗?
这个函数是怎么得到转过来的数??
function add(x,y) { return x + y; }
function subtract(x,y) { return x - y; }
function multiply(x,y) { return x * y; }
function divide(x,y) { return x / y; }function operate(operator, operand1, operand2)
{
return operator (operand1, operand2);
}var i = operate(add, operate(add, 2, 3), operate(multiply, 4, 5));var operators = {
add: function(x,y) { return x+y; },
subtract: function(x,y) { return x-y; },
multiply: function(x,y) { return x*y; },
divide: function(x,y) { return x/y; },
pow: Math.pow};function operate2(op_name, operand1, operand2)
{
if (typeof operators[op_name] == "function")
return operators[op_name](operand1, operand2);
else throw "unknown operator";
}var j = operate2("add", "hello", operate2("add", " ", "world"))
var k = operate2("pow", 10, 2)document.write(j);
document.write('<br>');
document.write(k);
</script>
我想问一下关于黄色字的问题是,为什么POW的TYPEOF是function?这里的function意思是指在operators 对象中存在的意思吗还有一个问题是蓝色字~math.pow是怎么分底数和次幂的?转的值是(10底数,2次幂),这个是内部定义的吗?
这个函数是怎么得到转过来的数??
因为pow是一个“函数”对象,所以返回function,就好像 a = 'str', a是string一样还有一个问题是蓝色字~math.pow是怎么分底数和次幂的?转的值是(10底数,2次幂),这个是内部定义的吗?
这个函数是怎么得到转过来的数??math是javascript的内置数学计算对象详情看看
http://www.w3school.com.cn/js/js_obj_math.asp
pow(10,2)=100.就是10的2次幂。哪个是底数哪个是幂数都是函数定义成这样的。
你看看C语言的pow就知道了。