(转)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 ...
随机推荐
- Cognos集成至portal平台运行报表时只出“#”
1. 问题描述 报表集成到平台后,运行报表过程中,当多次运行后,页面只显示“#” 2. 问题分析 这是因为浏览器筛选器限制问题 3. 解决方案 在IE浏览器设置中,Internet选项-安全-自定义级 ...
- 跟随我在oracle学习php(18)
修改表: 一般概述 通常,创建一个表,能搞定(做到)的事情,修改表也能做到.大体来说,就可以做到: 增删改字段: 增:alter table 表名 add [column] 字段名 字段类 ...
- Unity中使用百度中文语音识别功能
下面是API类 Asr.cs using System; using System.Collections; using System.Collections.Generic; using Unity ...
- tpot ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
机器学习训练的时候报出这个问题 是因为dataframe中的数据类型有一个是‘object’,把它转成int,或float 就行,如下 df['A'] = df['A‘].astype(int) 参考 ...
- [Leetcode 40]组合数和II Combination Sum II
[题目] Given a collection of candidate numbers (candidates) and a target number (target), find all uni ...
- from jobscrawler_qianchengwuyou.items import JobscrawlerQianchengwuyouItem
-- coding: utf-8 -- import scrapy from jobscrawler_qianchengwuyou.items import JobscrawlerQianchengw ...
- vim详解
vim介绍: 1.vim是vi的升级版本 2.vim是带有颜色显示的 3.vim三个模式:一般模式.编辑模式.命令模式 最小化模式下默认是没有安装vim的: [root@linux-xl ~]# yu ...
- 目标检测(二) SPPNet
引言 先简单回顾一下R-CNN的问题,每张图片,通过 Selective Search 选择2000个建议框,通过变形,利用CNN提取特征,这是非常耗时的,而且,形变必然导致信息失真,最终影响模型的性 ...
- 转载:入门Webpack,看这篇就够了
写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...
- Shiro-ini认证
#2019.2.2 shiro的ini认证 先用IDEA创建一个普通的MAVEN项目,并导入依赖 <!--Junit单元测试--> <groupId>junit</gro ...