博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用来枚举属性的对象工具函数
阅读量:2577 次
发布时间:2019-05-11

本文共 1055 字,大约阅读时间需要 3 分钟。

1.把p中可枚举的属性赋值到o,并返回o(若o和p中有同名属性则覆盖)

function extend(o,p){      for(prop in p){           o[prop]=p[prop]      }      return o}

2.把p中可枚举的属性赋值到o,并返回o(若o和p中有同名属性,o不受影响)  

function merge(o,p){      for(prop in p){           if(o.hasOwnProperty[prop]) continue           o[prop]=p[prop]      }      return o}

3.如果o中属性在p中没有同名属性,则从o中删除这个属性

function restrict(o,p){      for(prop in o){           if(!(prop in p))  delete o[prop]      }      return o}

4.如果o中属性在p中存在同名属性,则从o中删除这个属性

function subtract(o,p){      for(prop in p){           delete o[prop]      }      return o}

5.返回一个新对象,这个对象拥有同时在o和p中出现的属性,如果o和p中有重名属性,使用p中属性值

function union(o,p){   return extend(extend({},o),p)}

6.返回一个新对象,这个对象拥有同时在o和p中出现的属性,p中的属性值被忽略  

function intersection(o,p){   return restrict(extend({},o),p)}

7.返回一个数组,这个数组是o中可枚举的自有属性的名字  

function keys(o){  if(type of  o !== "object")  throw TypeError()  var result = []  for(var  prop in o){    if(o.haOwnProperty(prop))                result.push(prop)   }    return result}

  

  

  

  

转载于:https://www.cnblogs.com/wangxiayun/p/8086026.html

你可能感兴趣的文章
【Leetcode刷题篇】leetcode300 最长上升子序列
查看>>
【Leetcode刷题篇】leetcode394 字符串解码
查看>>
【Leetcode刷题篇】leetcode152 乘积最大数组
查看>>
【Leetcode刷题篇】leetcode56 合并区间
查看>>
【Leetcode刷题篇】leetcode210 课程表II
查看>>
【Leetcode刷题篇】leetcode207 课程表
查看>>
【Leetcode刷题篇】leetcode322 零钱兑换
查看>>
【Leetcode刷题篇】leetcode437 路径总和III
查看>>
【Linux篇】Linux常用命令之性能优化
查看>>
【面试篇】JVM体系
查看>>
【Leetcode刷题篇】leetcode406 根据身高重建队列
查看>>
【Leetcode刷题篇】leetcode581 最短无序连续子数组
查看>>
【Leetcode刷题篇】leetcode538 把二叉搜索树转换为累加树
查看>>
【多线程与高并发】线程的优先级是怎么回事?
查看>>
【多线程与高并发】Java守护线程是什么?什么是Java的守护线程?
查看>>
【Leetcode刷题篇/面试篇】-前缀树(Trie)
查看>>
【Leetcode刷题篇】leetcode337 打家劫舍III
查看>>
【Leetcode刷题篇】leetcode4 寻找两个正序数组的中位数
查看>>
【Leetcode刷题篇】leetcode316 去除重复字母
查看>>
【Leetcode刷题篇】leetcode1081 不同字符的最小子序列
查看>>