(转)es6中object.create()和object.assign()
今天学习javascript面向对象,在学习Obejct方法时了解到create方法,偶像想起之前使用的assign方法,顺带查找一番,感觉这篇博客讲解详细,遂转载。
先简单提一下装饰器函数,许多面向对象的语言都有修饰器(Decorator)函数,用来修改类的行为。目前,es6中有个提案将这项功能,引入了 ECMAScript。而在ts中则完全支持装饰器。这段时间看ng2看得到我头大。
Object.assing(target,…sources)
参考自微软的开发者社区。
用途:将来自一个或多个源对象中的值复制到一个目标对象。
语法:Object.assign(target, …sources );
- …sources,必需。从其中复制可枚举属性的对象。
- 异常: 如果存在分配错误,此函数将引发 TypeError,这将终止复制操作。如果目标属性不可写,则将引发 TypeError。
- 备注:null 或 undefined 源被视为空对象一样对待,不会对目标对象产生任何影响。
/*合并对象*/
- 例1,
var first = { name: "Bob" };
var last = { lastName: "Smith" };
var person = Object.assign(first, last);
console.log(person);/*{ name: 'Bob', lastName: 'Smith' }*/
/*克隆对象*/
例2,
var obj = { person: "Bob Smith"};
var clone = Object.assign({}, obj);
console.log(obj);/*{ person: 'Bob Smith' }*/
这里探究备注内容,"null 或 undefined 源被视为空对象一样对待,不会对目标对象产生任何影响。"
var test=null;
var test1=Object.assign({},test);
console.log(test1);/*{}*/
var test2=undefined;
var test4=Object.assign({},test2);
console.log(test4);/*{}*/
通过以上可以看出,test1和test4依然空对象,印证了备注里面的内容。
Object.create(prototype,descriptors)
用途:创建一个具有指定原型且可选择性地包含指定属性的对象。
参数:prototype 必需。 要用作原型的对象。 可以为 null。
descriptors 可选。 包含一个或多个属性描述符的 JavaScript 对象。
“数据属性”是可获取且可设置值的属性。 数据属性描述符包含 value 特性,以及 writable、enumerable 和 configurable 特性。 如果未指定最后三个特性,则它们默认为 false。 只要检索或设置该值,“访问器属性”就会调用用户提供的函数。 访问器属性描述符包含 set 特性和/或 get 特性。
- prototype 参数不是对象且不为 null。
- descriptors 参数中的描述符具有 value 或 writable 特性,并具有 get 或 set 特性。
- descriptors 参数中的描述符具有不为函数的 get 或 set 特性。
备注:若要停止原型链,可以使用采用了 null prototype 参数的函数。 所创建的对象将没有原型。
创建使用null原型的对象并添加两个可枚举的属性。
例1,
var newObj = Object.create(null, {
size: {
value: "large",
enumerable: true
},
shape: {
value: "round",
enumerable: true
}
});
document.write(newObj.size + "<br/>");/*large*/
document.write(newObj.shape + "<br/>");/*round*/
document.write(Object.getPrototypeOf(newObj));/*null*/
创建一个具有与 Object 对象相同的内部原型的对象。 该对象具有与使用对象文本创建的对象相同的原型。
Object.getPrototypeOf 函数可获取原始对象的原型。 若要获取对象的属性描述符,可以使用Object.getOwnPropertyDescriptor 函数
例2,
var firstLine = { x: undefined, y: undefined };
var secondLine = Object.create(Object.prototype, {
x: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
},
y: {
value: undefined,
writable: true,
configurable: true,
enumerable: true
}
});
document.write("first line prototype = " + Object.getPrototypeOf(firstLine));/*first line prototype = [object Object])*/
document.write("<br/>");
document.write("second line prototype = " + Object.getPrototypeOf(secondLine));/*first line prototype = [object Object]*/
创建一个具有与 Shape 对象相同的内部原型的对象。
例3,
// Create the shape object.
var Shape = { twoDimensional: true, color: undefined, hasLineSegments: undefined }; var Square = Object.create(Object.getPrototypeOf(Shape)); 要区分数据属性还是访问器属性。
以下是那数据属性作为例子说明,配合访问器属性的Object.create()用法暂时还木有搞定。
例4,
var Shape = { twoDimensional: true, color: undefined, hasLineSegments: undefined };
var Square = Object.create(Object.getPrototypeOf(Shape),{xiaoming:{
value:"hello,world",
writable:true,
}});
Square.xiaoming="xiaohua";
console.log(Square.xiaoming);/*xiaohua8*/
如果默认writable、enumerable、configurable都是false。
原文出处: http://www.onlyfordream.cn/2018/03/19/es6%E4%B8%ADobject-create%E5%92%8Cobject-assign/
(转)es6中object.create()和object.assign()的更多相关文章
- Object.create 和 Object.assign
Object.assign(target, ...source) 1.Object.assign方法只会拷贝源对象自身(不包括原型)的并且可枚举的属性到目标对象,使用源对象的get和目标对象的set, ...
- Object.create 以及 Object.setPrototypeOf
第一部分 Object.crate() 方法是es5中的关于原型的方法, 这个方法会使用指定的原型对象以及属性去创建一个新的对象. 语法 Object.create(proto, [ properti ...
- js中的new操作符与Object.create()的作用与区别
js中的new操作符与Object.create()的作用与区别 https://blog.csdn.net/mht1829/article/details/76785231 2017年08月06日 ...
- Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create
Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...
- Object.create()和new object()和{}的区别
Object.create()介绍 Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString ...
- js-new、object.create、bind的模拟实现【转载备忘】
//创建Person构造函数,参数为name,age function Person(name,age){ this.name = name; this.age = age; } function _ ...
- Object.create() 实现
if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototyp ...
- ES5 Object.create 方法
Object.create(proto[, propertiesObject])The Object.create() method creates a new object with the spe ...
- ES6中map和set用法
ES6中map和set用法 --转载自廖雪峰的官方网站 一.map Map是一组键值对的结构,具有极快的查找速度. 举个例子,假设要根据同学的名字查找对应的成绩,如果用Array实现,需要两个Arra ...
随机推荐
- ERP新人防坑指南
本文作为初入ERP行业的新人的防坑指南,讲解了一些常见犯的错,这样也少走一些弯路,如果你是老鸟,请绕过 :-) 本文关联的代码使用kotlin编写,请自行转换为c#.java等你熟悉的语言,表述的坑在 ...
- angular2学习笔记3
一.项目搭建 二.生成首页的4个tab页面 三.运行部署及配置
- .net core 连接mysql
Package引入: mysql.data MySql.Data.EntityFrameworkCore Pomelo.EntityFrameworkCore.MySql 2. 在package ma ...
- Java的四个标记接口:Serializable、Cloneable、RandomAccess和Remote接口
一.概述 标记接口是一些没有属性和方法的接口,也是一种设计思想.Java中的一个标记接口表示的的是一种类的特性,实现了该标记接口的类则具有该特性.如实现了Serializable接口的类,表示这个类的 ...
- Centos 配置mailx使用外部smtp发送邮件
安装mailx yum install mailx 配置mailx 笔者推荐163邮箱,当然,QQ邮箱也是可以的,PS:记得要进邮箱打开SMTP vi /etc/mail.rc //如果不存在,则编辑 ...
- result type
<result-types> <result-type name="chain" class="com.opensymphony.xwork2.Acti ...
- ImportError libcublas.so.9.0
What to do when you've installed cuda and tensorflow, but you get this error right after you import ...
- 五、docker网络技术
五 docker网络技术 1.本章环境: 源码文件目录: 2.网络基础回顾 通道: NAT将私有地址和端口号翻译成公有的地址和端口号项某网站发出数据包.某网站根据数据表查出私有ip和端口号返回数据. ...
- 小程序获取openid 出现null,{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"}
//根据微信提供的接口,请求得到openid和session_id public class UserInfoUtils { private String getKeyURL="https: ...
- Django知识总结(二)
拾 ● 模型(M) 一 ● ORM(对象关系映射, Object Relational Mapping) 类----一张表 类的实例对象----表中的一条记录 映射关系 ①python的类名对应的SQ ...