<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">从开始学到现在也有段时间了,在学习数据结构和算法的同时温习了下JS的基础知识。在这里简单总结下: </span>
1.switch
js中的switch条件可以是任意类型,而c++和java只能是整型
2.toFixed(2)
固定精度,可以四舍五入,比如:1.567.toFixed(2) => 1.57
3.递归
经典例子:
function factorial(number) {
if (number === 1) {return number;}
return number * factorial(number-1);
}
print(factorial(5)) => 120 //该递归层次较浅,当层次较深超出了js的能力时,可以用递归的迭代来解决。
note:
1)当一个函数被递归调用时,在递归结束之前,计算结果被暂时挂起。
2)任意被递归定义的函数,都可以被重写为迭代式的程序。
4.创建对象的方法(在一本书上看到的,但我之前并没有用过)
定义一个包含属性和方法的构造函数,并将方法紧跟在构造函数后面定义。
example:
function Checking(amount) {
this.balance = amount;
this.deposit = deposit;
this.withdraw = withdraw;
this.toString = toString;
}
function deposit (amount) {
this.balance+=amount;
}
function withdraw(amount) {
if (this.balance >= amount){this.balance -= amount;}
else {print("insufficient balance");}
}
function toString(){return "Balance is:" + this.balance;}
To be continued!!