定时器中的this

        function Aaa()
{
var _this = this; //解决关键
this.a = 12;
//但凡被定时器调的函数,this必然是window,所以,show里面的this.a是undefined的
// setInterval(this.show, 1000);
setInterval(function () { _this.show(); }, 1000);
} Aaa.prototype.show = function ()
{
console.log(this.a);
} var obj = new Aaa();
obj.show();

按钮事件中的this

        function Bbb()
{
var _this = this;
this.b = 5;
var btn = document.getElementById('btn1');
//同理,onclick下show函数中如果是this,则指的是按钮,this.show()将会是undefined,所以得存起来 //btn.onclick = (function () {
// this.show();
//}).bind(this); //或者 btn.onclick = function () {
_this.show();
} btn.ondblclick = function () {
console.log(this.value); //弹出按钮的显示文本
}
} Bbb.prototype.show = function () {
console.log(this.b);
} window.onload = function () {
new Bbb();
}
        document.write('Show this');
//输出: Show this
document.write('Show this');
//输出: window

作用域

        var fun = 90;
(function () {
//对于fun,首先本范围内上部没有,那么就找本范围内的函数,如果函数没有就找外部,
//如果外部没有就undefinded,不会因为下部有但不是函数就选择下部的
console.log(fun); //输出的是fun2的那个
function fun() {
console.log('我是fun1方法');
}
function fun() {
console.log('我是fun2方法');
}
var fun = 3;
console.log(fun); //3
})()
        var mmm = "da";
var obj = {
mmm: "xiao",
method: function () {
console.log(this.mmm); //xiao
function shakereturn() {
var mmm = "zuixiao";
console.log(this.mmm); //da
}
shakereturn(); (function shakereturn2() {
var mmm = "zuixiao2";
console.log(this.mmm); //da
})() }
}
obj.method();

参数绑定

        function attachfun(b, c) {
console.log(this.a + b + c);
}
attachfun(3, 4);//NaN
var bbb = { a: 20 };
attachfun.apply(bbb, [3, 4]);
attachfun.call(bbb, 3, 4);
var f_1 = attachfun.bind(bbb);
f_1(3, 4);
var f_2 = attachfun.bind(bbb, 3);
f_2(4);
        //可以使用bind来设置this达到this暂存的效果。
var bar = {
name: "bar",
body: document.getElementsByTagName("body")[0], greeting: function () {
console.log("Hi there, I'm " + this + ":" + this.name);
}, anotherMethod: function () {
this.body.addEventListener("click", (function () {
this.greeting();
}).bind(this));
}
}; bar.anotherMethod();
// Hi there, I'm [object Object]:bar

最新文章

  1. CI中的数据库操作
  2. argparse解析参数模块
  3. Android ORM应用开发框架KJFrameForAndroid使用详解
  4. 我心中的核心组件(可插拔的AOP)~分布式Session组件
  5. Leetcode 1 two sum 难度:0
  6. ListView的addHeaderView()方法相关问题
  7. HDU 4267 A Simple Problem with Integers --树状数组
  8. Launchpad添加openPGP keys
  9. Sprint会议-初步组织划分
  10. linux vi 编辑器命令
  11. Linux高级字符设备驱动
  12. Http状态码完整说明
  13. Lombok介绍及使用方法
  14. URAL 1404. Easy to Hack! (模拟)
  15. 内存排查 valgrind
  16. 转:Spring FactoryBean源码浅析
  17. Leancloud+Valine打造Hexo个人博客极简评论系统
  18. CentOS7通过rsync+crontab实现两台服务器文件同步
  19. Java并发编程笔记之ArrayBlockingQueue源码分析
  20. VMware-workstation12.5.6 新建虚拟机 安装 centos6.5

热门文章

  1. 【Codeforces Round #301 (Div. 2) C】 Ice Cave
  2. Setup iOS Development Environment.
  3. js页面载入特效如何实现
  4. Intellij IDEA中使用Debug
  5. x265探索与研究(四):怎样编码视频?
  6. Android官方数据绑定框架DataBinding(一)
  7. 1.2.4 Java Annotation 提要
  8. ios开发runtime学习四:动态添加属性
  9. [RxJS] Split an RxJS observable with window
  10. 【a703】求逆序对(树状数组的解法)