extend.js
/* * Copy the enumerable properties of p to o, and return o. * If o and p have a property by the same name, o's property is overwritten. * This function does not handle getters and setters or copy attributes. */ function extend(o, p) { for(prop in p) { // For all props in p. o[prop] = p[prop]; // Add the property to o. } return o; } /* * Copy the enumerable properties of p to o, and return o. * If o and p have a property by the same name, o's property is left alone. * This function does not handle getters and setters or copy attributes. */ function merge(o, p) { for(prop in p) { // For all props in p. if (o.hasOwnProperty[prop]) continue; // Except those already in o. o[prop] = p[prop]; // Add the property to o. } return o; } /* * Remove properties from o if there is not a property with the same name in p. * Return o. */ function restrict(o, p) { for(prop in o) { // For all props in o if (!(prop in p)) delete o[prop]; // Delete if not in p } return o; } /* * For each property of p, delete the property with the same name from o. * Return o. */ function subtract(o, p) { for(prop in p) { // For all props in p delete o[prop]; // Delete from o (deleting a // nonexistent prop is harmless) } return o; } /* * Return a new object that holds the properties of both o and p. * If o and p have properties by the same name, the values from o are used. */ function union(o,p) { return extend(extend({},o), p); } /* * Return a new object that holds only the properties of o that also appear * in p. This is something like the intersection of o and p, but the values of * the properties in p are discarded */ function intersection(o,p) { return restrict(extend({}, o), p); } /* * Return an array that holds the names of the enumerable own properties of o. */ function keys(o) { if (typeof o !== "object") throw TypeError(); // Object argument required var result = []; // The array we will return for(var prop in o) { // For all enumerable properties if (o.hasOwnProperty(prop)) // If it is an own property result.push(prop); // add it to the array. } return result; // Return the array. }
相关推荐
-
对象类型判断工具 javascript
2019-1-7
-
工作中常用到的JS验证 javascript
2019-1-8
-
canvas旋转图片 javascript
2019-1-7
-
前端JS转图片为base64并压缩、调整尺寸脚本 javascript
2019-1-8
-
转义textarea回车读取(/n)符号 javascript
2019-1-7
-
用JavaScript将数字转换为大写金额 javascript
2019-1-8
-
js导出excel javascript
2019-1-8
-
ajax方式验证登录系统 javascript
2019-1-8
-
JS判断当前页面是在 QQ客户端/微信客户端/iOS浏览器/Android浏览器/PC客户端 javascript
2019-1-8
-
JavaScript浮点运算,小数点精度 javascript
2019-1-7