js 基础回顾
2013-11-21 14:28
239 查看
1.循环Object的所有属性
另,判断是否为数组
3.判断对象类型
4. 复制对象
4.自执行函数的用法,以及在内层应用外层对象
var config = {verbose :false, filepaths :[], doSomthing :function(){}}; for(var i in config){ if(config.hasOwnProperty(i)){ // 语法点 alert(i); } }2.方法的参数为array类型,在方法内部对参数合法性的检验
function setLinters(linters){ if (!(linters instanceof Array) || linters.length === 0) { // 语法点 return false; }else{ return linters.length; } } setLinters([]); // false setLinters([1,'5']); // 2 setLinters(21); // false
另,判断是否为数组
Object.prototype.toString.call(arr)==='[object Array]'
3.判断对象类型
/* 检测对象类型 * @param: obj {JavaScript Object} * @param: type {String} 以大写开头的 JS 类型名 * @return: {Boolean} */ function is(obj, type) { return Object.prototype.toString.call(obj).slice(8, -1) === type; }
4. 复制对象
/* 复制对象 * @param: obj {JavaScript Object} 原始对象 * @param: isDeep {Boolean} 是否为深拷贝 * @return: {JavaScript Object} 返回一个新的对象 */ function copy(obj, isDeep) { var ret = obj.slice ? [] : {}, p; // 配合 is 函数使用 if(!isDeep && is(obj, 'Array')) return obj.slice(); for(p in obj) { var prop = obj[p]; if(!obj.hasOwnProperty(p)) continue; if(is(prop, 'Object') || is(prop, 'Array')) { ret[p] = copy(prop, isDeep); } else { ret[p] = prop; } } return ret; }
4.自执行函数的用法,以及在内层应用外层对象
var LintRoller = { version : '2.3', name : 'ouc wy', innerFun : function(){ var me = this; // 语法点1 (function(){ // 语法点2 alert(me.name); // 对外层的引用 'ouc wy' alert(this.name); // 当前对象作用域 '' alert(this.other); // undefined })(); } }; LintRoller.innerFun(); // 此处有一点疑问:第8、9行为什么不报引用错误?
var LintRoller = { version : '2.3', name : 'ouc wy', innerFun : function(){ var me = this; // 语法点1 (function(){ // 语法点2 alert(me.name); // 对外层的引用 'ouc wy' alert(this.name); // 'hello,word' alert(this.other); // undefined })(); } }; var name = 'hello,word'; LintRoller.innerFun();
相关文章推荐
- JS基础回顾(二)--数组
- js基础回顾-对象数组篇
- JS基础回顾,小练习(Event事件)
- 【JS基础回顾】Array对象及其常用属性以及方法
- JS基础回顾,小练习(去除字符串空格)
- js基础回顾
- js基础回顾-数据类型/表达式及运算符
- JS基础回顾,小练习(实现each方法)
- JS基础回顾,小练习(获取一个对象里面第一层元素的数量)
- JS基础回顾,小练习(DOM元素)
- js 基本基础知识回顾
- Javascript基础回顾之(二) js作用域
- 黑马程序员-JS基础知识回顾一------------几个基本概念
- Javascript基础回顾之(三) js面向对象
- Js基础回顾 - 函数作用域及js oop(1)
- JS基础回顾,小练习(克隆对象,数组)
- JS 正则表达式基础知识详解与回顾
- JS 基础回顾(一)
- 【JS基础回顾】String对象及其常用属性以及方法
- web前端面试中常见的js基础又实用的知识回顾