js深入理解(二)
来源:互联网 发布:淘宝3c认证必须要吗 编辑:程序博客网 时间:2024/05/29 13:03
1.函数闭包
var scope = "global scope";function checkScope (){ var scope = "local scope"; function f(){return scope;} return f;}//checkScope ()()输出local scopevar scope = "global scope";function checkScope (){ var scope = "local scope"; function f(){ return scope; } return f();}//checkScope()输出local scope2.with语句 ,将对象属性作用域链添加至顶层(在严格模式中不可用)
function Lakers() { this.name = "kobe bryant"; this.age = "28"; this.gender = "boy";}var people=new Lakers();with(people){ var str = "姓名: " + name + "<br>"; str += "年龄:" + age + "<br>"; str += "性别:" + gender;} (function (){ alert(str);}()); //输出 姓名: kobe bryant<br>年龄:28<br>性别:boy
3.为js添加断点
function ddd(o){if (o==undefined) {debugger;};}4.对象引用,引用值改变,原对象值也跟着变。
function a(){ //use constructorthis.user="user";this.pwd="pwd";this.get="get";return this;} var c = a();(function (){alert(c.user);var b = c;b.user = "ddd";alert(c.user);}());//输出:user ddd5.创建对象的几种方式。
//通过构造函数创建 var obj = new ddd("a1","a2"); function ddd (a1,a2,){ this.a1=a1; this.a2=a2; }//对象直接量 var obj = {a1:"ddd",a2:"ccc"}; //通过继承来创建 var a = {x:1,y:2}; var obj = Object.create(a);//obj继承了a
6.获取对象可枚举属性
//获取对象属性名称,返回数组Object.getOwnPropertyNames(obj);//获取对象属性,返回数组Object.keys(obj);
7.对象存取器属性
var o = {$n:0,//$暗示私有属性get getN(){return this.$n;},set setN(n){this.$n=n;}} ;(function (){o.setN=4;alert(o.getN);}());//输出 48.获取对象属性特性
//是否可读Object.getOwnPropertyDescriptor({x:1},"x").writable//获取属性值Object.getOwnPropertyDescriptor({x:1},"x").value//是否可枚举Object.getOwnPropertyDescriptor({x:1},"x").enumerable//是否可配置,该property的属性是否可以修改,property是否可以删除。Object.getOwnPropertyDescriptor({x:1},"x").configurable9.定义属性特性,不能定义继承的属性
var a = {x:1};Object.defineProperty(a,"x",{value:4,writable:false,configurable:true,enumerable:true});a.x = 3;alert(a.x);//输出 4//定义多个属性特性(function (){ var a = {x:1,y:2,$n:0,get getN(){return this.$n;}}; Object.defineProperties(a,{x:{value:4,writable:false,configurable:true,enumerable:true}, y:{value:4,writable:false,configurable:true,enumerable:true}, getN:{get:function(){return this.$n+2;},configurable:true,enumerable:true}}); a.y = 3; alert(a.getN);/输出 2}());
0 0
- js深入理解(二)
- 深入理解REST(二)
- 深入理解缓冲区(二)
- 《深入理解计算机系统》(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD 深入理解(二)
- GCD深入理解(二)
- GCD 深入理解(二)
- HDOJ1181变形课(DFS)
- input text表单中嵌入select选项的方法
- Chapter 5:collection view - iOS 8 Swift Programming cookBook 读书笔记
- hdoj2191多重背包化为01背包
- Java基础-常用API之String类
- js深入理解(二)
- ZOJ 2734--Exchange Cards【DFS经典】
- 82. Remove Duplicates from Sorted List II
- DPDK 内存管理(一)(内存初始化)
- hbase无法启动,The node /hbase is not in ZooKeeper和Can't get master address from ZooKeeper;
- 快速排序
- Static修饰成员变量
- Arbitrage (Uva 104 dp+floyd)
- HAProxy+Keepalived实现Web服务器负载均衡