1(6分)
實現type函數用于識別標準類型和內置對象類型,語法如下:
var t = type(obj);
使用舉例如下:
var t = type(1) // t==="number"
var t = type(new Number(1)) // t==="number"
var t = type("abc") // t==="string"
var t = type(new String("abc")) // t==="string"
var t = type(true) // t==="boolean"
var t = type(undefined) // t==="undefined"
var t = type(null) // t==="null"
var t = type({}) // t==="object"
var t = type([]) // t==="array"
var t = type(new Date) // t==="date"
var t = type(/\d/) // t==="regexp"
var t = type(function(){}) // t==="function"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code</title>
</head>
<body>
<script type="text/javascript">
function type(obj){
return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
}
console.log(type(1));
console.log(type(new Number(1)));
console.log(type("abc"));
console.log(type(new String("abc")));
console.log(type(true));
console.log(type(undefined));
console.log(type(null));
</script>
</body>
</html>```
>2(10分)
ES5中定義的Object.create(proto)方法,會創建并返回一個新的對象,這個新的對象以傳入的proto對象為原型。
語法如下:
Object.create(proto) (注:第二個參數忽略)
proto —— 作為新創建對象的原型對象
使用示例如下:
var a = Object.create({x: 1, y: 2});
alert(a.x);
Object.create在某些瀏覽器沒有支持,請給出Object.create的兼容實現。
#如何兼容??什么意思??
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code</title>
</head>
<body>
<script type="text/javascript">
Object.create=Object.create || function(obj){
var F=function(){};
F.prototype=obj;
return new F();
}
}
var a = Object.create({x: 1, y: 2});
console.log(a.x);
</script>
</body>
</html>```
3(10分)
高版本的firefox,chrome及ie10以上的瀏覽器實現了Function.prototype.bind方法,bind方法調用語法為:
functionObj.bind(thisArg[, arg1[, arg2[, ...]]])
使用范例參考如下:
function move(x, y) {
this.x += x;
this.y += y;
}
var point = {x:1, y:2};
var pointmove = move.bind(point, 2, 2);
pointmove(); // {x:3, y:4}```
但是低版本瀏覽器中并未提供該方法,請給出兼容低版本瀏覽器的bind方法的代碼實現。
>4
(10分)
斐波那契數列(Fibonacci Sequence)由 0 和 1 開始,之后的斐波那契數就由之前的兩數相加。在數學上,斐波那契數列是以遞歸的方法來定義的:

請實現一個函數,參數為n,返回結果為以n為下標的斐波那契數。函數語法為
var num = fibonacci(n);
使用舉例如下
var num = fibonacci(3); // num值等于2
var num = fibonacci(5); // num值等于5