js学习笔记,仅供自己使用!
javascript// 最大安全整数
var big = Number.MAX_VALUE
console.log(big)
//
var min = Number.MIN_VALUE
console.log(min)
1.7976931348623157e+308 5e-324 undefined
javascriptvar infin = Number.POSITIVE_INFINITY
console.log(infin)
Infinity undefined
javascript1/0
Infinity
javascripta = Number.NaN
NaN
javascriptconsole.log(Number.parseFloat('123234.xx')) // 将字符串转换成浮点数
console.log(Number.parseFloat('123234.11'))
123234 123234.11 undefined
javascriptconsole.log(Number.parseInt('111.222')) // 将字符串转换成整型
111 undefined
javascriptNumber.isFinite(23213232.223) // 判断传入的数字是否为有限数字
true
javascriptNumber.isInteger(23213232.223) // 判断传入的数字是否为整型
false
javascriptNumber.isNaN(23213232.223) // 判断传入的数字是否为NaN
false
数学计算相关函数 使用连接: https://www.w3school.com.cn/js/jsref_obj_math.asp
javascriptvar pi_value=Math.PI;
var sqrt_value=Math.sqrt(15);
console.log(pi_value)
console.log(sqrt_value)
console.log(Math.abs(-1.3)) // 求绝对值
3.141592653589793 3.872983346207417 1.3 undefined
与或非 && ! ||
!== |=== :严格相对,不做隐式转换
< >= <= != ==
javascriptconsole.log(100>'200') // f
console.log(300>'200') // t
console.log(300>'2000') //f
console.log(300>'2a') //f
console.log('3000'>'2000') //t
// 宽松
console.log(300=='300') // t
console.log('300'=='300') //t
// 严格
console.log(300==='300') // f
console.log('300'==='300') //t
false true false false true true true false true undefined
表达式1?表达式2:表达式3 (true|false)?“true”,
javascript(1)?'a':'b'
'a'
javascriptconsole.log('a' instanceof String) //判断是否属于指定的类型
a = new String('b')
console.log(a instanceof String) // instanceof 需要构建个onj才能使用
false true undefined
javascriptconsole.log(typeof('a')) // 判断对象的类型
string undefined
delete 删除对象、属性、数组元素 in 判断指定的属于在对象内,在返回true
javascriptlet trees = new Array('A','BAY','cc',1)
for (var i=0;i<trees.length;i++)
console.log(trees[i])
console.log(trees.length)
A BAY cc 1 4 undefined
语法 function* 函数名(参数){函数体}
javascriptfunction* gogo(){
i = 0
while (true) {
yield i++
}
}
gen = gogo()
console.log(gen.next().value)
console.log(gen.next().value)
console.log(gen.next().value)
0 1 2 undefined
javascriptvar parts = ['s','k']
var l = ['a',...parts,'b']
l
[ 'a', 's', 'k', 'b' ]
javascriptfunction f(x,y,z) {
console.log(x+y+z)
}
var a = [2,3,4]
f(...a)
9 undefined
javascriptfor (let x=1;x<10;x++)
{
line = '';
//console.log('aa',x) x = 1
for (let y=1;y<=x;y++){ // y = 1
//console.log(y)
line += ` ${y}*${x}=${x*y} `
if (x==y)
console.log(line)
}
}
1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=16 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 undefined
function 函数名(参数){ 函数体 }
javascriptfunction add(x,y){
return x+y
}
add(1,-1)
// 简单函数
0
javascriptvar a = function add(x,y){
return x+y
}
console.log(a(1,1))
//函数表达式内的函数名称只能内部使用,可以省略
//
2 undefined
javascript// 求和函数
x = 10
var sum = function _sum(x){
result = 0
while(x){
result+=x
x --
}
console.log(result)
}
sum(x)
55 undefined
javascript// 递归求和函数
x = 10000
var sum = function _sum(n){
let result = 0
if (n==1) return 1;
return result += n +_sum(--n)
console.log(result)
}
sum(x)
50005000
匿名函数
javascriptvar a = function (x,y){
return x+y
}
console.log(a(1,1))
2 undefined
输入或者输出是函数的函数被称为高阶函数
javascriptfunction a(){
console.log('a')
return function b(){
console.log('b')
}
}
undefined
javascriptc= a()
c()
a b undefined
javascriptlet counter = function(){
let i = 0;
function inc(){
return ++i
}
return inc;
}
c= counter()
console.log(c())
console.log(c())
console.log(c())
console.log(c())
console.log(c())
1 2 3 4 5 undefined
javascriptlet counter = function(){
let i = 0;
return ++i
}
c = counter()
console.log(c)
console.log(c)
console.log(c)
1 1 1 undefined
javascriptfunction add(x,y){
return x+y
}
a = add
a(1,2)
3
javascript
本文作者:mykernel
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!