// This example is from the book _JavaScript: The Definitive Guide_. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // This example is provided WITHOUT WARRANTY either expressed or implied. // You may study, use, modify, and distribute it for any purpose. // We define some simple functions here 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; } // Here's a function that takes one of the above functions // as an argument and invokes it on two operands function operate(operator, operand1, operand2) { return operator(operand1, operand2); } // We could invoke this function like this to compute // the value (2+3) + (4*5): var i =index.html operate(add, operate(add, 2, 3), operate(multiply, 4, 5)); // Now we store the functions defined above in an associative array var operators = new Object(); operators["add"] = add; operators["subtract"] = subtract; operators["multiply"] = multiply; operators["divide"] = divide; operators["pow"] = Math.pow; // works for predefined functions too. // This function takes the name of an operator, looks up // that operator in the array, and then invokes it on the // supplied operands. Note the syntax used to invoke the // operator function. function operate2(op_name, operand1, operand2) { if (operators[op_name] == null) return "unknown operator"; else return operators[op_name](operand1, operand2); } // We could invoke this function as follows to compute // the value ("hello" + " " + "world"): var j = operate2("add", "hello", operate2("add", " ", "world")) // Using the predefined Math.pow() function var k = operate2("pow", 10, 2)
file: /Techref/language/java/script/definitive/examples/6.2.txt, 1KB, , updated: 1997/4/4 11:59, local time: 2024/8/22 22:22,
3.146.152.196:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://piclist.com/tecHREF/language/java/script/definitive/examples/6.2.txt"> language java script definitive examples 6</A> |
Did you find what you needed? |
![]() o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.