1、Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。 ES2015引入的

,且可用polyfilled。要支持旧浏览器的话,可用使用jQuery.extend或者_.assign()。

2、Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象。

语法:

Object.create(proto[, propertiesObject])

实现类式继承

function MyClass() {
SuperClass.call(this);
OtherSuperClass.call(this);
} // 继承一个类
MyClass.prototype = Object.create(SuperClass.prototype);
// 混合其它
Object.assign(MyClass.prototype, OtherSuperClass.prototype);
// 重新指定constructor
MyClass.prototype.constructor = MyClass; MyClass.prototype.myMethod = function() {
// do a thing
};  

使用 Object.create 的 propertyObject参数

var o;

// 创建一个原型为null的空对象
o = Object.create(null); o = {};
// 以字面量方式创建的空对象就相当于:
o = Object.create(Object.prototype); o = Object.create(Object.prototype, {
// foo会成为所创建对象的数据属性
foo: {
writable:true,
configurable:true,
value: "hello"
},
// bar会成为所创建对象的访问器属性
bar: {
configurable: false,
get: function() { return 10 },
set: function(value) {
console.log("Setting `o.bar` to", value);
}
}
}); function Constructor(){}
o = new Constructor();
// 上面的一句就相当于:
o = Object.create(Constructor.prototype);
// 当然,如果在Constructor函数中有一些初始化代码,Object.create不能执行那些代码 // 创建一个以另一个空对象为原型,且拥有一个属性p的对象
o = Object.create({}, { p: { value: 42 } }) // 省略了的属性特性默认为false,所以属性p是不可写,不可枚举,不可配置的:
o.p = 24
o.p
//42 o.q = 12
for (var prop in o) {
console.log(prop)
}
//"q" delete o.p
//false //创建一个可写的,可枚举的,可配置的属性p
o2 = Object.create({}, {
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true
}
});

  

Polyfill

这个 polyfill 涵盖了主要的应用场景,它创建一个已经选择了原型的新对象,但没有把第二个参数考虑在内。

请注意,尽管在 ES5 中 Object.create支持设置为[[Prototype]]null,但因为那些ECMAScript5以前版本限制,此 polyfill 无法支持该特性。

if (typeof Object.create !== "function") {
Object.create = function (proto, propertiesObject) {
if (typeof proto !== 'object' && typeof proto !== 'function') {
throw new TypeError('Object prototype may only be an Object: ' + proto);
} else if (proto === null) {
throw new Error("This browser's implementation of Object.create is a shim and doesn't support 'null' as the first argument.");
} if (typeof propertiesObject != 'undefined') throw new Error("This browser's implementation of Object.create is a shim and doesn't support a second argument."); function F() {}
F.prototype = proto; return new F();
};
}

_.assign

js:https://lodash.com/vendor/cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js

function Foo() {
this.a = 1;
} function Bar() {
this.c = 3;
} Foo.prototype.b = 2;
Bar.prototype.d = 4; _.assign({ 'a': 0 }, new Foo, new Bar);
// => { 'a': 1, 'c': 3 }

_.assignIn

function Foo() {
this.a = 1;
} function Bar() {
this.c = 3;
} Foo.prototype.b = 2;
Bar.prototype.d = 4; _.assignIn({ 'a': 0 }, new Foo, new Bar);
// => { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }

  

Object的方法的更多相关文章

  1. C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法

    在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...

  2. Object.assign方法复制或合并对象

    Object.assign() 方法可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象 var obj = { a: 1 }; var copy = Object.assign({ ...

  3. Object.assign()方法

    对象的扩展 1.ES6中,对象的属性和方法可简写:对象的属性值可不写,前提是属性名已经声明: var name = "zhangsan"; "; var obj = { ...

  4. -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 解决方法

    -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 解决方法: 错误:NSMutableD ...

  5. findByExample(Object exampleEntity)方法得到的List判断是否为空,不可用(lis != null)

    用findByExample(Object exampleEntity)方法可以应用在用户登录上面,获得有登陆名和密码的user对象进行查询. 返回两者都符合的对象列表,为空则登陆失败. 错误的方法: ...

  6. [Guava官方文档翻译] 5. Guava的Object公共方法 (Common Object Utilities Explained)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3537367.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  7. 6.类似Object监视器方法的Condition接口

    在<1.有关线程.并发的基本概念>中,我们利用synchronized关键字.Queue队列.以及Object监视器方法实现了生产者消费者,介绍了有关线程的一些基本概念.Object类提供 ...

  8. Object.defineProperties()和Object.defineProperty()方法

    Object.defineProperty() 方法会直接在一个对象上定义一个新属性,或者修改一个对象的现有属性, 并返回这个对象. 语法:Object.defineProperty(obj, pro ...

  9. Object.clone()方法与对象的深浅拷贝

    转载:[https://www.cnblogs.com/nickhan/p/8569329.html] 引言 在某些场景中,我们需要获取到一个对象的拷贝用于某些处理.这时候就可以用到Java中的Obj ...

随机推荐

  1. datanode启动不起来的各种原因

    一般在数据节点的log日志信息里能找到导致启动不起来的原因. 1.Namenode和Datanode的NamenodeID不一致 描述:一般在集群多次重新格式化HDFS之后,或者刚安装时会碰到.日志信 ...

  2. 01-python中字符串的常见操作

    (1)find 检测str是否包含在myStr中,如果存在则返回开始的索引值,否则返回-1. In [1]: myStr = "hello world tairan and tairanCi ...

  3. OkHttp使用教程——网络操作之OkHttp, Volley以及Gson

    写这篇文章的动机 在安卓项目中有一个问题可能无法避免:网络.不管你是加载图片,请求API数据还是从因特网上获得一个字节,你都是在使用网络. 鉴于网络在安卓中的重要性与基础性,当今安卓开发者面临的问题之 ...

  4. Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around

    1.错误描述 INFO:2015-05-01 11:12:15[localhost-startStop-1] - Root WebApplicationContext: initialization ...

  5. tcp为什么要三次握手,而不能二次握手?

    谢希仁版<计算机网络>中的例子是这样的,"已失效的连接请求报文段"的产生在这样一种情况下:client发出的第一个连接请求报文段并没有丢失,而是在某个网络结点长时间的滞 ...

  6. (十七)java冒泡排序和compareto

    java中的排序有:冒泡排序.快速排序.选择排序.插入排序和希尔排序,还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等.     冒泡排序法:利用双重for循环,重复走访要排序的数列,两两比较大 ...

  7. JBOD磁盘磁盘簇

    JBOD是存储领域中一类重要的存储设备. JBOD(Just a Bunch Of Disks,磁盘簇)是在一个底板上安装的带有多个磁盘驱动器的存储设备.通常又称为Span. 和RAID阵列不同,JB ...

  8. python之文件读写详解

    打开文件 函数open() 参数说明: file:文件路径 mode: 文件的读写方式,默认'r',只读方式: buffering:设置缓冲策略,0用于二进制文件,1为行缓冲,用于文本模式:默认二进制 ...

  9. freemarker获取封装类中对象的属性(六)

    freemarker获取封装类中对象的属性 1.设计思路 (1)封装学生类 (2)创建数据模型 (3)新建student.ftl (4)运行Junit测试文件,生成HTML文件 2.封装学生类 Stu ...

  10. tablesorter 的使用

    <table id="myTable" class="tablesorter"> <thead> <tr> <th&g ...