-
斐波那契数列 [1,1,1,3,5,9,17]判断第n项的值(递归)
https://www.cnblogs.com/superlizhao/p/11603158.html
//数学公式总结
function fib(n){
if(n>0&&n<4){
return 1}
return 2**(n-3)+1
//最差的方法当属递归,因为每个值都需要堆栈储存空间
function loop(n){
let res=0;
if(n<4){
return 1
}else{
res=loop(n-1)+loop(n-2)+loop(n-3)
}
return res
}
loop(4)//3
``` //1、1、2、3、5、8、13、21第n项合
//数学方法
//递归方法
function fibonacci(n) {
if (n == 1 || n == 2) {
return 1
};
return fibonacci(n - 2) + fibonacci(n - 1);
}
//优化递归方法
function fibonacci(n) {
function fib(n, v1, v2) {
if (n == 1)
return v1;
if (n == 2)
return v2;
else
return fib(n - 1, v2, v1 + v2)
}
return fib(n, 1, 1)
}
fibonacci(30)
//方法三:改进递归-利用闭包特性把运算结果存储在数组里,避免重复计算
var fibonacci = function () {
let memo = [0, 1];
let fib = function (n) {
if (memo[n] == undefined) {
memo[n] = fib(n - 2) + fib(n - 1)
}
return memo[n]
}
return fib;
}()
fibonacci(30)
//方法三1:改进递归-摘出存储计算结果的功能函数
var memoizer = function (func) {
let memo = [];
return function (n) {
if (memo[n] == undefined) {
memo[n] = func(n)
}
return memo[n]
}
};
var fibonacci=memoizer(function(n){
if (n == 1 || n == 2) {
return 1
};
return fibonacci(n - 2) + fibonacci(n - 1);
})
fibonacci(30)
//循环
function fibonacci(n) {
var n1 = 1, n2 = 1, sum;
for (let i = 2; i < n; i++) {
sum = n1 + n2
n1 = n2
n2 = sum
}
return sum
}
fibonacci(30)
//循环
var fibonacci = function (n) {
let n1 = 1; n2 = 1;
for (let i = 2; i < n; i++) {
[n1, n2] = [n2, n1 + n2]
}
return n2
}
fibonacci(30)
-
状态码
-
100:
-
101:
-
200 :
-
204:
-
206:
-
301 :
-
302 :
-
303 :
-
304 :
-
307 :
-
400 :
-
401 :
-
403 :
-
404 :
-
500 :
-
503 :
-
100:
浏览器缓存设置
-
判断值类型
var a = "string.",b = 123,c= [1,2,3],d = new Date(),e=null;f = function(){this.name="22";}; // typeof typeof a ------------> string typeof b------------> number typeof c------------> object typeof d------------> object typeof e------------> object typeof f------------> function // instanceof c instanceof Array---------------> true d instanceof Date---------------> true f instanceof Function------------> true f instanceof function------------> false //constructor在继承类下容易出错 c.constructor === Array----------> true d.constructor === Date-----------> true e.constructor === Object-------> true function A(){}; function B(){}; A.prototype = new B(); //A继承自B var aObj = new A(); aobj.constructor === B -----------> true; aobj.constructor === A -----------> false; //prototype Object.prototype.toString.call(a) ==='[object String]'-------> true; Object.prototype.toString.call(b) ==='[object Number]'-------> true; Object.prototype.toString.call(c) ==='[object Array]' -------> true; Object.prototype.toString.call(d) ==='[object Date]' -------> true; Object.prototype.toString.call(e) ==='[object Null]' -------> true; Object.prototype.toString.call(f) ==='[object Function]' -------> true; //Jquery jQuery.type( undefined ) === "undefined" jQuery.type() === "undefined" jQuery.type( window.notDefined ) === "undefined" jQuery.type( null ) === "null" jQuery.type( true ) === "boolean" jQuery.type( 3 ) === "number" jQuery.type( "test" ) === "string" jQuery.type( function(){} ) === "function" jQuery.type( [] ) === "array" jQuery.type( new Date() ) === "date" jQuery.type( new Error() ) === "error" // as of jQuery 1.9 jQuery.type( /test/ ) === "regexp"
-
手动实现js的call, apply, bind, instanceof方法
/** * 实现一个caLl方法 */ Function.prototype.mycall = function(context) { //判断是否传入指定的对象 context = context || window; //拿到传入的参数 let args = [...arguments].slice(1); //把调用的函数添加到对象中 context.fn = this; //执行函数 let result = context.fn(...args); //执行函数后,从对象中删除掉函数 delete context.fn; return result; } function Test() { console.log(this.name); console.log(this.sex); } let obj = { name: '李四', sex: '男' } Test.mycall(obj)//李四 男 /** * 实现apply方法 * 大体实现方法和apply相同 */ Function.prototype.myapply = function(context) { context = context || window; context.fn = this; let result; if (arguments[1]) { result = context.fn(...arguments[1]); } else { result = context.fn(); } delete context.fn; return result; } function fn(name, sex) { this.name = name; this.sex = sex; console.log(this.name); console.log(this.sex); } let obj = {}; fn.myapply(obj,['张三','女']);//张三 女 /** * 实现一个bind函数 */ Function.prototype.mybind = function(context) { if (typeof this !== 'function') { return new Error("不是一个函数"); } let _this = this; //保存当前函数 let args = [...arguments].slice(1); return function F(...newArgs) { //bind返回的是个函数,所以可以实例化一个对象返回 if (this instanceof F) { return new _this(...args,...newArgs); } else { return _this.apply(context,args.concat(newArgs)); } } } function parent(sex) { console.log(sex); console.log(this.name); } let Son = { name: 'zhangsan' } let son = parent.mybind(Son,'男'); son();//男 张三 /** * 实现一个instanceof的判断方法 * instanceof方法的判断是通过隐式原型属性__proto__判断的 * 只要在这条原型链上的对象都为true */ function myInstanceoF(left, right) { //保存左边构造函数的原型对象 let prototype = right.prototype; left = left.__proto__; while (true) { if (left == null) { return false; } else if (left == prototype) { return true; } left = left.__proto__; } } function Hello(name) { this.name = name; } let test = new Hello('张三'); let arr = new Array('33'); console.log(myInstanceoF(arr, Array)); console.log(myInstanceoF(test, Hello)); console.log(myInstanceoF(arr, Hello));