常用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的最小整數(shù) Math.cos(num) : 返回num的余弦值 Math.exp(x) : 返回以自然數(shù)為底,x次冪的數(shù) Math.floor(num) : 返回小于num的最大整數(shù) Math.log(num) : 返回num的自然對數(shù) Math.max(num1,num2) : 返回num1和num2中較大的一個 Math.min(num1,num2) : 返回num1和num2中較小的一個 Math.pow(x,y) : 返回x的y次方的值 Math.random() : 返回0到1之間的一個隨機數(shù) Math.round(num) : 返回num四舍五入后的值 Math.sin(num) : 返回num的正弦值 Math.sqrt(num) : 返回num的平方根 Math.tan(num) : 返回num的正切值 Math.E : 自然數(shù)(2.718281828459045) Math.LN2 : 2的自然對數(shù)(0.6931471805599453) Math.LN10 : 10的自然對數(shù)(2.302585092994046) Math.LOG2E : log 2 為底的自然數(shù)(1.4426950408889634) Math.LOG10E : log 10 為底的自然數(shù)(0.4342944819032518) Math.PI : π(3.141592653589793) Math.SQRT1_2 : 1/2的平方根(0.7071067811865476) Math.SQRT2 : 2的平方根(1.4142135623730951) Number型 常用的數(shù)字函數(shù) 聲明 var i = 1;var i = new Number(1); 字符串與數(shù)字間的轉(zhuǎn)換 var i = 1;var str = i.toString(); //結(jié)果: “1”var str = new String(i); //結(jié)果: “1”i = parseInt(str); //結(jié)果: 1i = parseFloat(str); //結(jié)果: 1.0//注意: parseInt,parseFloat會把一個類似于”32G”的字符串,強制轉(zhuǎn)換成32 判斷是否為有效的數(shù)字 var i = 123; var str = “string”;if( typeof i == “number” ){ } //true//某些方法(如:parseInt,parseFloat)會返回一個特殊的值NaN(Not a Number)//請注意第2點中的[注意],此方法不完全適合判斷一個字符串是否是數(shù)字型!!i = parseInt(str);if( isNaN(i) ){ } 數(shù)字型比較 //此知識與[字符串比較]相同- **小數(shù)轉(zhuǎn)整數(shù)**var f = 1.5;var i = Math.round(f); //結(jié)果:2 (四舍五入)var i = Math.ceil(f); //結(jié)果:2 (返回大于f的最小整數(shù))var i = Math.floor(f); //結(jié)果:1 (返回小于f的最大整數(shù)) 格式化顯示數(shù)字 var i = 3.14159;//格式化為兩位小數(shù)的浮點數(shù)var str = i.toFixed(2); //結(jié)果: “3.14”//格式化為五位數(shù)字的浮點數(shù)(從左到右五位數(shù)字,不夠補零)var str = i.toPrecision(5); //結(jié)果: “3.1415” X進制數(shù)字的轉(zhuǎn)換 var i = parseInt(“0x1f”,16);var i = parseInt(i,10);var i = parseInt(“11010011”,2); 隨機數(shù) //返回0-1之間的任意小數(shù)var rnd = Math.random();//返回0-n之間的任意整數(shù)(不包括n)var rnd = Math.floor(Math.random() * n) 5.Regex //在這個最大的對象的原型上加一個extends方法 使得下面所有的原型 都有這個方法 //這個原型的作用是通過迭代 復(fù)制傳進來的構(gòu)造器的所有的原型的方法 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];//復(fù)制傳進來的構(gòu)造器的所有的原型的方法給當前正在調(diào)用這個方法的對象 } } function Person(name){ this.name = name; } Person.prototype = { eat:function(){ alert(this.name+"在吃飯"); } }; function Father(name){ this.name = name; } Father.extends(Person);//extends方法是最大的對象Object加的方法 所有的子孫 構(gòu)造器都有這個方法 var f = new Father("小頭爸爸"); f.eat(); 一些圖示 文/poetries(簡書作者) 原文鏈接:http://www.jianshu.com/p/1f2314552e23
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

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

Hot Article
Guide: Stellar Blade Save File Location/Save File Lost/Not Saving
4 weeks ago
By DDD
Oguri Cap Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Agnes Tachyon Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Dune: Awakening - Advanced Planetologist Quest Walkthrough
4 weeks ago
By Jack chen
Date Everything: Dirk And Harper Relationship Guide
4 weeks ago
By Jack chen

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)
