this
来源:互联网 发布:数组初始化 编辑:程序博客网 时间:2024/06/16 06:47
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>this</title></head><body><script>/* * 1.this是指当前方法或者属性所在的对象 * 2.注意:1)避免在函数中多层使用this * 2)避免数组调用使用this * 3)避免回调函数中使用this * * */var person={role:'parent',child:{son:function(){console.log(this); //this指向person.child}}};person.child.son();var o={f1:function(){console.log(this); //指向ovar f2=function(){console.log(this); //指向window}();}};o.f1();//修改var o={f1:function(){console.log(this); var that=this;var f2=function(){console.log(that); }();}};o.f1();var counter = {count: 0,inc:function () {//'use strict';//在严格模式下this指向顶层环境,会报错this.count++;console.log(this);}};var f = counter.inc; //相当于把counter.inc放在全局环境中,然后再执行 this指向windowf();counter.inc();//直接执行counter.inc()方法this指向counter对象var arr_obj={v:'hello',p:['a','b'],f:function(){var that=this;this.p.forEach(function(item){console.log(that.v+' '+item);});}};arr_obj.f();//找出数组中的最大值var arr=[1,2,3,5,9,3];console.log(Math.max.apply(this,arr));//将类似数组的对象转化成数组console.log(Array.prototype.slice.apply({0:5,length:1}));//利用call调用对象的原生方法var obj_pro={}console.log(obj_pro.hasOwnProperty('toString'));obj_pro.toString=function(){return true;};console.log(obj_pro.hasOwnProperty('toString'));//Object的原生方法已经被改变console.log(Object.prototype.hasOwnProperty('toString'));//解决:使得在obj_pro作用域中运行hasOwnProperty()方法Object.prototype.hasOwnProperty().call(obj_pro,'toString');</script></body></html>
阅读全文
0 0
- this
- this
- this
- this
- this
- this
- this
- this
- this
- this
- this
- $(this)
- this
- this
- this
- this
- $(this)
- this
- 对寄存器ESP和EBP的一些理解
- Spring:IoC 用法(四、Java配置用法)
- java编程
- Trafodion 查看历史SQL执行情况
- caffemodel进行批量测试
- this
- 矩阵十大经典题目
- 棋盘问题
- CSDN个人博客不小心删除了怎么办
- fopen code
- IT 行业加班到底有没有价值?
- 统计Xcode整个工程的代码行数
- 用Hashtable实现简单的cache缓存
- foreach引用传值注意