以前不太理解面向对象的this指向问题,今天自己看着视频教程,加自己学了2个例子,终于明白点了。

我们在写对象程序的时候,我们希望保持this始终是指向对象的,但事实确常常事与愿违。

正常情况this的指向都没问题,比如下面

 1 //构造函数
2 function createPerson(name,age){
3 this.name=name;
4 this.age=age;
5 this.showName();
6 }
7 //原型方法
8 createPerson.prototype.showName=function(){
9 console.log('我的名字是:'+this.name+'我的年纪是:'+this.age);
10 }
11 //调用结果为 我的名字是:程序员我的年纪是:27
12 new createPerson('程序员','27');
13
14 //可以看到这里的this始终指向 createPerson对象

但工作的写代码不会那么简单alert一个值,常常会加入事件等等,这时候this的指向是怎样的呢?还会指向对象么?看下面

 1 function tabSwitch(id){
2 this.oDiv=document.getElementById(id);
3 this.btn=this.oDiv.getElementsByTagName('input');
4 this.div=this.oDiv.getElementsByTagName('div');
5 }
6 tabSwitch.prototype.tab=function(){
7 for(var i=0;i<this.btn.length;i++){
8 this.btn[i].index=i;
9 this.btn[i].onclick=function(){
10 alert(this);//object HTMLInputElement
11 }
12
13 }
14 }

看到了么this,变成了html的一个节点,这时候再继续写下边的代码,肯定就错了。这时候我需要改下this的指向,让this重新指向对象。继续

 1 function tabSwitch(id){
2 this.oDiv=document.getElementById(id);
3 this.btn=this.oDiv.getElementsByTagName('input');
4 this.div=this.oDiv.getElementsByTagName('div');
5 }
6 tabSwitch.prototype.tab=function(){
7 //把对象中的this存下来赋值为_this
8 var _this=this;
9 for(var i=0;i<this.btn.length;i++){
10 this.btn[i].index=i;
11 this.btn[i].onclick=function(){
12 alert(_this);//object
13 }
14
15 }
16 }

用_this变量缓存指向对象的this就可以在正确的地方 用到正确的指向。(有点绕晕了)

最后上一个今天尝试些的复杂一点点的例子:左右点击按钮滑动切换ul

 1 function slideMove(moveUl,arrowLeft,arrowRight,marginRight){
2 this.moveUl=$('#'+moveUl);
3 this.liLength=$('#'+moveUl).find('li').length;
4 this.liWidth=$('#'+moveUl+'>li').eq(0).innerWidth()+marginRight;
5 this.arrowLeft=$('#'+arrowLeft);
6 this.arrowRight=$('#'+arrowRight);
7 this.path=0;
8 this.moveUl.css('width',this.liWidth*this.liLength);
9 this.init();//初始化
10 }
11 slideMove.prototype.init=function(){
12 var _this=this;//对象
13 this.arrowLeft.on('click',function(){
14 _this.clickLeft();
15 });
16 this.arrowRight.on('click',function(){
17 _this.clickRight();
18 });
19 }
20 slideMove.prototype.clickLeft=function(){
21
22 console.log(this.path)
23 //到左边了return掉
24 if(this.path<=0){
25 this.path=0;
26 return false;
27 }
28 this.path--;
29 this.moveUl.stop().animate({'left':-this.path*this.liWidth});
30 }
31 slideMove.prototype.clickRight=function(){
32
33 console.log(this.path)
34 //到了右边return掉
35 if(this.path>=this.liLength-4){
36 this.path=this.liLength-3;
37 return false;
38 }
39 this.path++;
40 this.moveUl.stop().animate({'left':-this.path*this.liWidth});
41 }
42 //调用
43 var slide1=new slideMove('moban_ul1','arrow_left1','arrow_right1',22);

this指向的更多相关文章

  1. C语言中 指向函数的指针 简介

    引子:在学习CPrimerPlus的第十四章的14.13节中,遇到了如下三行文字,是有关指向函数的指针的,把我搞晕了. char * fump(); //返回指向char的指针的函数 char (* ...

  2. JS this指向

    正常模式 在正常模式下独立函数的的 this 指向 undefined 或 window. <script type="text/javascript"> functi ...

  3. java多态性,父类引用指向子类对象

    父类引用指向子类对象指的是: 例如父类Animal,子类Cat,Dog.其中Animal可以是类也可以是接口,Cat和Dog是继承或实现Animal的子类. Animal animal = new C ...

  4. 【javascript 技巧】谈谈setTimeout的作用域以及this的指向问题

    setTimeout的用法详见:http://www.w3school.com.cn/htmldom/met_win_settimeout.asp 是的,setTimeout的常见用法是让某个方法延迟 ...

  5. what's this? 浅谈js中this的指向问题

    刚刚学习js的朋友可能和我一样,看到代码中的this总是一脸懵逼,不知道this到底指向谁.经过一段时间的了解,我想跟大家分享下自己的理解. 何时出现this 函数在调用的时候,会自动获得两个特殊变量 ...

  6. EC笔记,第二部分:10.让=返回指向*this的引用

    Effective C++ 学习笔记 10 让=返回指向*this的引用 Table of Contents 1. 原因 2. 建议:在没有充分理由标新立异前,最好的做法是遵从传统. –by SkyF ...

  7. JavaScript中this指针指向的彻底理解

    this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象 这一点与函数中自由变量Action-varibal不同 var ...

  8. JavaScript中this指向的简单理解

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

  9. 12-返回指针的函数&&指向函数的指针

    前言 接下来我只讲指针的最常见用法,比如这一章的内容----返回指针的函数 与 指向函数的指针   一.返回指针的函数 指针也是C语言中的一种数据类型,因此一个函数的返回值肯定可以是指针类型的. 返回 ...

  10. 彻底理解js中this的指向,不必硬背。

    首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象(这句话有些问题,后面会解释为什么会有问题,虽然 ...

随机推荐

  1. linux查找进程,查找僵死进程,查找僵死进程并自动杀掉

    查找进程: ps -aux | grep  flume  /  netstat -anop | grep 8080(端口号) 常规杀进程: kill  pid 查看僵死进程: ps -A -o sta ...

  2. Convert Sorted List to Binary Search Tree [LeetCode]

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  3. Discuz中解决jquery 冲突的方法 绝对简单

    将jquery.js在common.js之前载入,不然jquery的$()函数会覆盖common.js的$()函数: 然后用到jQuery的$()函数的地方都用jQuery()代替. 例如 $(doc ...

  4. php知识案列1

    用PHP,在1-20间随机产生5个不重复的值,如何做 复制代码 代码如下: <?php function NoRand($begin=0,$end=20,$limit=5){ $rand_arr ...

  5. 中文Locale

    sudo apt-get install locales dpkg-reconfigure locales 查看当前已安装locale: locale -a 查看locale设置: locale

  6. Spring AOP基本概念

    Spring AOP基本概念 目录 Spring AOP定义 AOP基本术语 通知类型 AOP定义 AOP基本术语 切面( Aspect ):一个能横切多个对象的模块化的关注点.对Spring AOP ...

  7. DynamicJson

    json字符串解析成Dynamic对象,开源地址http://dynamicjson.codeplex.com/,访问比较慢.使用方法摘录如下: Project Descriptiondynamic ...

  8. react-native执行 npm install cl.exe找不到 的问题

    最近在学习react-native,昨天在尝试某个demo时,执行 npm instal, 总是遇到 cl.exe文件找不到,最开始以为Microsoft Visual C++ 2015 Redist ...

  9. server and client

    server: using System;using System.Collections.Generic;using System.Linq;using System.Text;using Syst ...

  10. OpenGl在VS中的配置

    刚开始接触OpenGl的时候难免会遇到一些问题,这些问题可能和程序无关,只是一些编译环境的设置和头文件的安装,特别整理了一下,如下: (1)将gult32.dll,glut.dll复制到windows ...