js面试整理
2017-06-06 16:57
232 查看
1.判断基本数据类型typeof 判断对象的类型 Object.prototype.toString.call()
2.即时执行函数 IIFE
function foo1()
{
return {
bar: "hello"
};
}
function foo2()
{
return
{
bar: "hello"
};
}
console.log(foo1());
console.log(foo2());//undefined
4.闭包能够访问外部作用域的变量和参数
(function(x) {
return (function(y) {
console.log(x);
})(2)
})(1); //1
5.对象调用
var hero = {
_name: 'John Doe',
getSecretIdentity: function (){
return this._name;
}
};
var stoleSecretIdentity = hero.getSecretIdentity;
console.log(stoleSecretIdentity);
console.log(hero.getSecretIdentity());
// 将 getSecretIdentity 赋给 stoleSecretIdentity,等价于定义了 stoleSecretIdentity 函数:
// var stoleSecretIdentity = function (){
// return this._name;
// }
// stoleSecretIdentity
// 的上下文是全局环境,所以第一个输出 undefined。若要输出 John Doe,则要通过 call 、apply 和 bind 等方式改变 stoleSecretIdentity 的this 指向(hero)。
// 第二个是调用对象的方法,输出 John Doe。
//使用 typeof bar === "object" 判断 bar 是不是一个对象弊端? //使用 typeof 的判断Object弊端是显而易见的(这种弊端同使用 instanceof): let obj = {}; let arr = []; console.log(typeof obj === 'object'); //true console.log(typeof arr === 'object'); //true console.log(typeof null === 'object'); //true //从上面的输出结果可知,typeof bar === "object" 并不能准确判断 bar 就是一个 Object。可以通过 Object.prototype.toString.call(bar) === "[object Object]" 来避免这种弊端: let obj = {}; let arr = []; console.log(Object.prototype.toString.call(obj)); //[object Object] console.log(Object.prototype.toString.call(arr)); //[object Array] console.log(Object.prototype.toString.call(null)); //[object Null]
2.即时执行函数 IIFE
(function(){ var a = b = 3; })(); console.log(b); //3 全局变量 console.log(a); //undefined
for(var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, 1000); } //上面的输出并不是你以为的0,1,2,3,4,而输出的全部是5,这时 IIFE 就能有用了: for(var i = 0; i < 5; i++) { (function(i) { setTimeout(function() { console.log(i); }, 1000); })(i) }3.对于 return 、break、continue 等语句,如果后面紧跟换行,后面一定要加分号;
function foo1()
{
return {
bar: "hello"
};
}
function foo2()
{
return
{
bar: "hello"
};
}
console.log(foo1());
console.log(foo2());//undefined
4.闭包能够访问外部作用域的变量和参数
(function(x) {
return (function(y) {
console.log(x);
})(2)
})(1); //1
5.对象调用
var hero = {
_name: 'John Doe',
getSecretIdentity: function (){
return this._name;
}
};
var stoleSecretIdentity = hero.getSecretIdentity;
console.log(stoleSecretIdentity);
console.log(hero.getSecretIdentity());
// 将 getSecretIdentity 赋给 stoleSecretIdentity,等价于定义了 stoleSecretIdentity 函数:
// var stoleSecretIdentity = function (){
// return this._name;
// }
// stoleSecretIdentity
// 的上下文是全局环境,所以第一个输出 undefined。若要输出 John Doe,则要通过 call 、apply 和 bind 等方式改变 stoleSecretIdentity 的this 指向(hero)。
// 第二个是调用对象的方法,输出 John Doe。
相关文章推荐
- 前端面试答案整理之js
- 前端web优化和js常见面试内容整理
- JS+FLASH幻灯片播放图片脚本,整理了代码,使得调用更加方便!
- java面试试题(整理)
- IBM历年笔试、面试合集(站长整理)
- js常用整理收集(3)
- DWR util.js 整理(DWR 处理各种form表单Select/option,table等,List,Bean
- Js 整理
- 33中JS效果整理
- js常用整理收集(2)
- 整理:数据结构面试大全
- java面试整理(一)
- [导入]发布本人整理的面试问题大全,为准备找工作的同行们尽一份力!希望大家多补充或回答!
- [导入]发布本人整理的面试问题大全,为准备找工作的同行们尽一份力!希望大家多补充或回答!
- 评估密码强度JS脚本(整理部分)
- [导入]发布本人整理的面试问题大全,为准备找工作的同行们尽一份力!希望大家多补充或回答!
- java面试整理(二)
- 发布本人整理的面试问题大全,为准备找工作的同行们尽一份力!希望大家多补充或回答!
- sql server 面试时 的考题!!(整理)
- js整理工具