ES5 object方法整理
Object.getPrototypeOf(object):调用对象父类原型上的方法;
function Person(){
this.method1 = function(){alert(1)}
}
Person.prototype.method2 = function(){alert(2);} function Man(){
this.m1 = function(){
Object.getPrototypeOf(this).method1();
}
}
Man.prototype = new Person(); Man.prototype.m2 = function(){
Object.getPrototypeOf(this).method2();
} var man = new Man();
man.m1();
man.m2();
Object.getOwnPropertyDescriptor(object, propertyname):获取对象中属性的ECMAScript对象;
var obj = {}; obj.a = "abc"; var descriptor = Object.getOwnPropertyDescriptor(obj, "a"); for(var prop in descriptor){
document.write(prop + ': ' + descriptor[prop]);
document.write("<br />");
} /*
configurable: true
enumerable: true
value: abc
writable: true
*/
Object.defineProperty(object, propertyname, descriptor):将ECMAScript对象设置为对象中的属性.
var obj = {}; obj.a = "abc"; var descriptor = Object.getOwnPropertyDescriptor(obj, "a");
descriptor.writable = false;
Object.defineProperty(obj, "a", descriptor);
for(var prop in descriptor){
document.write(prop + ': ' + descriptor[prop]);
document.write("<br />");
}
/*
configurable: true
enumerable: true
value: abc
writable: false
*/
object.defineProperties(object, descriptors):用 ECMAScript对象 设置为object中多个属性的值.
var obj = {}; obj.a = "abc"; Object.defineProperties(obj,{
a:{
configurable: true,
enumerable: true,
value: 'aaa',
writable: false
}
});
var descriptor = Object.getOwnPropertyDescriptor(obj, "a");
for(var prop in descriptor){
document.write(prop + ': ' + descriptor[prop]);
document.write("<br />");
}
/*
configurable: true
enumerable: true
value: aaa
writable: false
*/
Object.getOwnPropertyNames(object):返回一个由对象属性名组成的数组(包含不可枚举的)
function a(){
this.a='1';
}
a.prototype.b='2';
var c=new a();
c.c='3';
alert(Object.getOwnPropertyNames(c));//a,c
Object.create(prototype, descriptors):建立一个原型为[prototype](必需,可为NULL),[descriptors](可选)为ECMAScript对象的对象.
var a = Object.create({a:1,b:2}, {
c: {
value: "large",
enumerable: true
},
d: {
value: "round",
enumerable: true
}
});
Object.seal(object):锁定对象,无法修改对象的属性,无法加入新的属性.并把ECMAScript对象的configurable设置为false;
var obj = {}; obj.a = "abc";
Object.seal(obj);
var descriptor = Object.getOwnPropertyDescriptor(obj, "a"); for(var prop in descriptor){
document.write(prop + ': ' + descriptor[prop]);
document.write("<br />");
} /*
configurable: false
enumerable: true
value: abc
writable: true
*/
Object.freeze(object):冻结对象,无法修改对象的属性,无法加入新的属性.
(与seal的区别为,freeze会把对象的数据属性的Writable设置为false)
var obj = {}; obj.a = "abc";
Object.freeze(obj);
var descriptor = Object.getOwnPropertyDescriptor(obj, "a"); for(var prop in descriptor){
document.write(prop + ': ' + descriptor[prop]);
document.write("<br />");
} /*
configurable: false
enumerable: true
value: abc
writable: false
*/
Object.preventExtensions(object):避免加新属性加入对象(Extensible设置为false);
var obj = { a: "1"}; Object.preventExtensions(obj);
document.write(Object.isExtensible(obj));//false obj.newProp = 50;
document.write(obj.newProp);//undefined
Object.isSealed(object);
Object.isFrozen(object);
Object.isExtensible(object); 判断对象是否为锁定,冻结,不可扩展的.(如果一个对象是冻结的,那其肯定是密封的);
var obj = { a: "1"};
//Object.seal(obj);
Object.freeze(obj);
//Object.preventExtensions(obj); alert(Object.isSealed(obj));
//alert(Object.isFrozen(obj));
//alert(Object.isExtensible(obj));
Object.keys(object):返回一个由对象可枚举的属性组成的数组.
function a(){
this.a='1';
}
var b=new a();
alert(Object.keys(b));//a
ES5 object方法整理的更多相关文章
- 【AS3】Flash与后台数据交换四种方法整理
随着Flash Player 9的普及,AS3编程也越来越多了,所以这次重新整理AS3下几种与后台数据交换方法.1.URLLoader(URLStream)2.FlashRemoting3.XMLSo ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- 在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码
[实例简介] 涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码 [实例截图] [核心代码] execScript方式: 1 2 3 4 5 6 7 8 9 1 ...
- 项目中常用js方法整理common.js
抽空把项目中常用js方法整理成了common.js,都是网上搜集而来的,大家一起分享吧. var h = {}; h.get = function (url, data, ok, error) { $ ...
- Ruby数组方法整理
数组方法整理 方法列表: all().any().none()和one():测试数组中的所有或部分元素是否满足给定条件.条件可以是语句块中决定,也可以是参数决定 append():等价于push() ...
- Vue2.x源码学习笔记-Vue实例的属性和方法整理
还是先从浏览器直观的感受下实例属性和方法. 实例属性: 对应解释如下: vm._uid // 自增的id vm._isVue // 标示是vue对象,避免被observe vm._renderProx ...
- [Guava学习笔记]Basic Utilities: Null, 前置条件, Object方法, 排序, 异常
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3842433.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Tomcat 多项目部署方法整理
Tomcat 多项目部署方法整理 说明:tomcat-deploy-aaa和tomcat-deploy-bbb是两个不同的web项目,为了方便以下简称aaa和bbb,请先自行创建并跑通 导航: NO1 ...
- Javascript Array 方法整理
Javascript Array 方法整理 Javascript 数组相关方法 说明 大多数其它编程语言不允许改变数组大小,越界访问索引会报错,但是 javascript不会报错,不过不建议直接修改a ...
随机推荐
- Quartz.Net_表达式参考说明
字段名 允许的值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日 1-31 , - * ? / L W C 月 1-12 , - * / ...
- PHP中SimpleXMLElement对象字符编码
最近在使用SimpleXMLElement来生成和解析XML. 由于我们使用PHP开发的这边使用UTF-8编码,而对方使用GBK编码,因此就遇到了中文字符编码问题. 后来发现,XML内部的编码与其头 ...
- Aizu-1378- ICPC Asia 2017-Secret of Chocolate Poles
Secret of Chocolate Poles Time Limit : 1 sec, Memory Limit : 262144 KB Problem A Secret of Chocolate ...
- Linux/Windows 平台最容易安装 Composer教程
我们采用的是全局安装方式,这样的话,就能够在命令行窗口中直接执行 composer 命令了. Mac 或 Linux 系统: 打开命令行窗口并执行如下命令将前面下载的 composer.phar 文件 ...
- CSS3 文本溢出问题
一.单行: 效果: 实现这各效果必须要加上: text-overflow: ellipsis; /*文本溢出*/ overflow: hidden; /*配合使用:溢出隐藏*/ white-space ...
- Domain 表达式的用法
什么是Domain [('create_uid','=',user.id)] Domain是个多条件的列表,每个条件是一个三元表达式:[(字段名,操作符,值), (字段名,操作符,值)] Domain ...
- 使用chart.js時取消懸浮在圖表頂部的'undefined'標識
解決方法:在options中設置legend項中display屬性為false options: { scales: { yAxes: [{ ticks: { beginAtZero: true } ...
- google hack使用集锦
转载:https://blog.csdn.net/weixin_42127015/article/details/84472777 关于google hack的几个基础过滤器使用[请务必谨记,过滤器虽 ...
- element-ui table多选CheckBox参数解析
element-UI里的table表格与多选框CheckBox的组合很常用,官网也给了很多参数,自己总结了一下,方便日后使用 1.简易用法,没有附加的功能 要在表格里使用CheckBox很简单,只需设 ...
- easygui.py的安装和下载地址
easygui下载地址:http://nchc.dl.sourceforge.net/project/easygui/0.97/easygui-0.97.zip 安装:解压后将easygui.py拷贝 ...