JavaScript Simple calculator can beautify _javascript skill

Source: Internet
Author: User
Tags eval javascript eval
JS Calculator code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>javascript simple calculator </title> <script type=" Text/javascript "> <!--Hide The Script from old browsers--function compute (OBJ)//Click functions that are called by the ' = ' button; {obj.expr.value = eval (obj.expr.value)}//Use the JS eval () method to compute the formula in name expr var one = ' 1 ' var two = ' 2 ' var three = ' 3 ' var four = ' 4 ' var five = ' 5 ' var six = ' 6 ' var seven = ' 7 ' var eight = ' 8 ' var nine = ' 9 ' var zero = ' 0 ' var plus = ' + ' var minus = '-' var multiply = ' * ' var divide = '/' var decimal = '. function Enter (obj,string)//type= call to "button" {obj.expr.value + = string}//The corresponding character is appended to the value of the form expr//--end hiding here--&G T </script> </pead> <body> <form name= "Calc" > <table border= "1" > &LT;TD colsPan= "4" ><input type= "text" name= "expr" size= "" action= "compute (this.form)"/></td> <tr> &LT;TD ><input type= "button" value= "7" onclick= "Enter (This.form,seven)"/></td> <td><input-type= " Button "Value=" 8 "onclick=" Enter (this.form,eight) "/></td> <td><input type=" button "value=" 9 "oncli ck= "Enter (this.form,nine)"/></td> <td><input type= "button" value= "/" onclick= "Enter (This.form, Divide) "/></td> </tr> <tr> <td><input type=" button "value=" 4 "onclick=" Enter (this.form , four) "/></td> <td><input type= button" value= "5" onclick= "Enter (this.form,five)"/></td > <td><input type= "button" value= "6" onclick= "Enter (this.form,six)"/></td> <td><input Type= "button" value= "*" onclick= "Enter (this.form,multiply)"/></td> </tr> <tr> <td><in Put type= "button" value= "1" onclick= "enter" (tHis.form,one) "/></td> <td><input type= button" value= "2" onclick= "Enter (this.form,two)"/>< /td> <td><input type= "button" value= "3" onclick= "Enter (this.form,three)"/></td> <td>< Input type= "button" value= "-" onclick= "Enter (This.form,minus)"/></td> </tr> <tr> <td colspan = "2" ><input type= "button" value= "0" onclick= "Enter (This.form,zero)"/></td> <td><input type= "Button" value= ". "Onclick=" Enter (this.form,decimal) "/></td> <td><input type=" button "value=" + "onclick=" Enter ( This.form,plus) "/></td> </tr> <tr> <td colspan=" 2 "><input type=" button "value=" = "Oncl ick= "Compute (this.form)"/></td> <td colspan= "2" ><input type= "button" value= "AC" onclick= " Form.reset () "/></td> </tr> </table> </form> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Description

JavaScript eval () function
Definitions and usage
The eval () function computes a string and executes the JavaScript code in it.

return value
The value, if any, obtained by calculating the string.

Description
The method accepts only the original string as a parameter, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object as an argument for the eval () function.

If you attempt to overwrite the Eval attribute or assign the eval () method to another property and call it through this property, the ECMAScript implementation allows a Evalerror exception to be thrown.

Thrown
Throws an SyntaxError exception if there are no valid expressions and statements in the argument.

If Eval () is invoked illegally, a Evalerror exception is thrown.

If the Javascript code passed to eval () generates an exception, eval () passes the exception to the caller.

Tips and comments
Tip: Although the eval () is very powerful, it is not much used in practice.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.