Common—M API—ath object type, Number type
常用API——Math对象型、Number型 Math.abs(num) : 返回num的绝对值 Math.acos(num) : 返回num的反余弦值 Math.asin(num) : 返回num的反正弦值 Math.atan(num) : 返回num的反正切值 Math.atan2(y,x) : 返回y除以x的商的反正切值 Math.ceil(num) : 返回大于num的最小整数 Math.cos(num) : 返回num的余弦值 Math.exp(x) : 返回以自然数为底,x次幂的数 Math.floor(num) : 返回小于num的最大整数 Math.log(num) : 返回num的自然对数 Math.max(num1,num2) : 返回num1和num2中较大的一个 Math.min(num1,num2) : 返回num1和num2中较小的一个 Math.pow(x,y) : 返回x的y次方的值 Math.random() : 返回0到1之间的一个随机数 Math.round(num) : 返回num四舍五入后的值 Math.sin(num) : 返回num的正弦值 Math.sqrt(num) : 返回num的平方根 Math.tan(num) : 返回num的正切值 Math.E : 自然数(2.718281828459045) Math.LN2 : 2的自然对数(0.6931471805599453) Math.LN10 : 10的自然对数(2.302585092994046) Math.LOG2E : log 2 为底的自然数(1.4426950408889634) Math.LOG10E : log 10 为底的自然数(0.4342944819032518) Math.PI : π(3.141592653589793) Math.SQRT1_2 : 1/2的平方根(0.7071067811865476) Math.SQRT2 : 2的平方根(1.4142135623730951) Number型 常用的数字函数 声明 var i = 1;var i = new Number(1); 字符串与数字间的转换 var i = 1;var str = i.toString(); //结果: “1”var str = new String(i); //结果: “1”i = parseInt(str); //结果: 1i = parseFloat(str); //结果: 1.0//注意: parseInt,parseFloat会把一个类似于”32G”的字符串,强制转换成32 判断是否为有效的数字 var i = 123; var str = “string”;if( typeof i == “number” ){ } //true//某些方法(如:parseInt,parseFloat)会返回一个特殊的值NaN(Not a Number)//请注意第2点中的[注意],此方法不完全适合判断一个字符串是否是数字型!!i = parseInt(str);if( isNaN(i) ){ } 数字型比较 //此知识与[字符串比较]相同- **小数转整数**var f = 1.5;var i = Math.round(f); //结果:2 (四舍五入)var i = Math.ceil(f); //结果:2 (返回大于f的最小整数)var i = Math.floor(f); //结果:1 (返回小于f的最大整数) 格式化显示数字 var i = 3.14159;//格式化为两位小数的浮点数var str = i.toFixed(2); //结果: “3.14”//格式化为五位数字的浮点数(从左到右五位数字,不够补零)var str = i.toPrecision(5); //结果: “3.1415” X进制数字的转换 var i = parseInt(“0x1f”,16);var i = parseInt(i,10);var i = parseInt(“11010011”,2); 随机数 //返回0-1之间的任意小数var rnd = Math.random();//返回0-n之间的任意整数(不包括n)var rnd = Math.floor(Math.random() * n) 5.Regex //在这个最大的对象的原型上加一个extends方法 使得下面所有的原型 都有这个方法 //这个原型的作用是通过迭代 复制传进来的构造器的所有的原型的方法 Object.prototype.extends = function(parent){ //console.log(parent.prototype); for(var prop in parent.prototype){ //console.log(prop);//eat extends this.prototype[prop] = parent.prototype[prop];//复制传进来的构造器的所有的原型的方法给当前正在调用这个方法的对象 } } function Person(name){ this.name = name; } Person.prototype = { eat:function(){ alert(this.name+"在吃饭"); } }; function Father(name){ this.name = name; } Father.extends(Person);//extends方法是最大的对象Object加的方法 所有的子孙 构造器都有这个方法 var f = new Father("小头爸爸"); f.eat(); 一些图示 文/poetries(简书作者) 原文链接:http://www.jianshu.com/p/1f2314552e23
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Assassin's Creed Shadows - How To Find The Blacksmith And Unlock Weapon And Armour Customisation
1 months ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1387
52

