javascript的创建对象object.create()和属性检测hasOwnPrototype()和propertyIsEnumerable()
Object.create("参数1[,参数2]")是E5中提出的一种新的对象的创建方式.
第一个参数是要继承到新对象原型上的对象;
第二个参数是对象属性.这个参数可选,默认为false
第二个参数的具体内容:
writable:是否可任意写, true可以,false不可以
configuration:是否能够删除,是否能够被修改.
enumerable:是否能用for in枚举
value:属性值
get()读
set()写
Object.create=function(obj){
var F=function(){};
F.prototype=obj;
return new F();
};
//再来一段代码演示参数的使用.
function Parent(){}
var p=new Parent();
var newObj=Object.create(p,{
name:{
value:"思思博士",//name值
writable:true,//可写
enumerable:false//不可枚举
},
age:{
get:function(){return "我今年"+age+"岁";},//读取age
set:function(val){age=val;},//设置age
configuration:false//能否删除
}
});
console.log("newobj的属性name:"+newObj.hasOwnProperty("name"));//false
console.log("parent的属性name:"+p.hasOwnProperty("name"));//false
上面一段代码是创建一个newObj对象,这个对象的原型是继承自parent
又有false可以推断出name是来自于原型中,并且不是来自于p这个对象.
hasOwnProperty().方法用来检测给定的名字是否是对象的只有属性.对于继承属性它将返回false
var o={x:1};
console.log(o.hasOwnProperty("x"));//true.
console.log(o.hasOwnProperty("y"));//false
console.log(o.hasOwnProperty("toString"));//false
propertyIsEnumerable()是hasOwnProperty()的增强版,只有检测到是只有属性且这个属性的可枚举为true是它才返回true.
var obj=Object.create(o);
obj.xx=1;
console.log(obj.propertyIsEnumerable("x"));//false
console.log(obj.propertyIsEnumerable("xx"));//true
console.log(Object.prototype.propertyIsEnumerable("toString"));//false
javascript的创建对象object.create()和属性检测hasOwnPrototype()和propertyIsEnumerable()的更多相关文章
- JavaScript中四种不同的属性检测方式比较
JavaScript中四种不同的属性检测方式比较 1. 用in方法 var o = {x:1}; "x" in o; //true "y" in o; //fa ...
- Object.create() __https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create
Object.create() 方法会使用指定的原型对象及其属性去创建一个新的对象. 语法 Object.create(proto[, propertiesObject]) 参数 proto 新创建对 ...
- js创建对象 object.create()用法
Object.create()方法是ECMAScript 5中新增的方法,这个方法用于创建一个新对象.被创建的对象继承另一个对象的原型,在创建新对象时可以指定一些属性. 语法: Object.crea ...
- [Javascript] Prototype 2 Object.create()
function Fencepost (x, y, postNum){ this.x = x; this.y = y; this.postNum = postNum; this.connections ...
- firefox-Developer开发者站点——关于Object.create()新方法的介绍
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/create Objec ...
- js Object.create 初探
1.作用 Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. https://developer.mozilla.org/zh-CN/docs/W ...
- 前端开发者进阶之ECMAScript新特性【一】--Object.create
Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数:prototype 必需. 要用作原型的对象. 可以为 nul ...
- Object.create() 的含义:从一个实例对象,生成另一个实例对象
出处:https://wangdoc.com/javascript/oop/object.html#objectcreate 生成实例对象的常用方法是,使用new命令让构造函数返回一个实例.但是很多时 ...
- ECMAScript新特性【一】--Object.create
Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数: prototype 必需. 要用作原型的对象. 可以为 nu ...
随机推荐
- Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息
1. 加载大图片到内存,从gallery获取图片 android默认的最大堆栈只有16M, 图片像素太高会导致内存不足的异常, 需要将图片等比例缩小到适合手机屏幕分辨率, 再加载. 从gallery ...
- org.springframework.beans.factory.BeanCreationException: sqlSessionFactory
sqlSessionFactory实例化错误 pom默认导入的jar包中存在低版本,导致实例化sqlSessionFactory错误,删除此jar包即可
- Gitlab项目用ssh克隆
Gitlab项目用ssh克隆 1. 新建一个文件夹并用git bash here 打开 2. 在git bash here 输入命令行 ssh-keygen –t rsa –C“邮箱名” 3. ...
- JAVA模块以及未来(转)
Java 9,OSGi以及模块化的未来 Java 9.OSGi以及模块化的未来(第二部分) Java 9终于要包含Jigsaw项目了
- EBS 定义并发参数常用值集
1.ORG_ID 2.DATE 3.YES_NO
- access_token和微信服务地址的获取
access_token的获取: //获取微信服务器地址:
- win32程序应用mfc库
引入<afx.h> 此时会出现如下错误: #ifdef _DLL#ifndef _AFXDLL#error Building MFC application with /MD[d] (CR ...
- istringstream、ostringstream、stringstream类介绍
本文转载自: http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html 一.C++的输入输出分为三种: (1)基于控制台的I/O ( ...
- New Concept English Two 6 13
$课文11 礼尚往来 105. I was having dinner at a restaurant when Tony Steele came in. 我正在一家饭馆吃饭,托尼.斯蒂尔走了进来. ...
- 5.4完成其他模块的xadmin后台注册
courses adminx.py from .models import Course, Lesson, Video, CourseResource import xadmin class Cou ...