说出this的三个应用

1.this是javascript中的一个关键字,它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用。 2.随着函数使用场合的不同,this的值会发生变化,但是有一个原则,就是this指的是调用函数的那个对象。 3.this分类 1)纯粹的函数调用 这时的this指向window。(函数没有所属对象:指向全局对象) eg: var x=1; function test(){ alert(this.x); } test();

 2)作为对象方法的调用 这时的this指向上级对象。(函数有所属对象时:指向所属对象) eg: function test(){ alert(this.x) } var o={}; o.x=1; o.m=test; o.m(); 3)作为构造函数调用 构造函数,就是通过这个函数生成一个新对象。这时的this指向这个新对象。(构造器中的 this:指向新对象) eg: function test(){ this.x=1; } var o=new test(); alert(o.x);

 var x=2; function test(){ this.x=1; } var o=new test(); alert(x);

 function foo(){return this.a}; var a=2; var o={a:3,foo:foo}; var p={a:4}; console.log(o.foo()); p.foo=o.foo; console.log(p.foo()); console.log(foo());

 4)apply或者call的调用 是改变函数对象的一个方法,作用是改变函数的调用对象。 eg: var x=0; function test(){ alert(this.x); } var o={}; o.x=1; o.m=test; o.m(); o.m.apply();

例题
//1 var point={     x:0,     y:0,     moveTo:function(x,y){         this.x=this.x+x;             this.y=this.y+y;             alert(this.x);    //1          alert(this.y);  //1     } } point.moveTo(1,1);

 //2    var myObject = {value: 100};    myObject.getValue = function () {        console.log(this.value);        console.log(this);        return this.value;    };    console.log(myObject.getValue());   // 100 object{value: 100} 100 

 //3    var myObject = {value: 100};    myObject.getValue = function () {        var foo = function () {            console.log(this.value);            console.log(this);        };        foo();        return this.value;    };    console.log(myObject.getValue());  //undefined window 100

 //4    var SomeClass = function(){        this.value = 100;    }    var myCreate = new SomeClass();    console.log(myCreate.value);     //100

 //5    var myObject = {value: 100};    var foo = function(){        console.log(this);    };    foo();          //window    foo.apply(myObject);  //object{value: 100}    foo.call(myObject);   //object{value: 100}

 //6var name = "clever coder";var person = {    name : "foocoder",    hello : function(sth){        var sayhello = function(sth) {            console.log(this.name + " says " + sth);        };        sayhello(sth);    }}person.hello("hello world");  //clever coder says hello world

 //7var name = "clever coder";var person = {    name : "foocoder",    hello : function(sth){        var that = this;        var sayhello = function(sth) {            console.log(that.name + " says " + sth);        };        sayhello(sth);    }}person.hello("hello world");  ////foocoder says hello world
//1 var point={     x:0,     y:0,     moveTo:function(x,y){         this.x=this.x+x;         this.y=this.y+y;         console.log(this.x);         console.log(this.y);     } }; point.moveTo(1,1);  //1 1
//2   var myObject = {value: 100};   myObject.getValue = function () {       console.log(this.value);       console.log(this);       return this.value;   };   console.log(myObject.getValue());  //100 object 100
//3   var myObject = {value: 100};   myObject.getValue = function () {       var foo = function () {           console.log(this.value);           console.log(this);       };       foo();       return this.value;   };   console.log(myObject.getValue());//undefined window 100
//4   var SomeClass = function(){       this.value = 100;   }   var myCreate = new SomeClass();   console.log(myCreate.value);  //100
//5   var myObject = {value: 100};   var foo = function(){       console.log(this);   };   foo();   //window   foo.apply(myObject);//Object   foo.call(myObject); //Object
 //6var name = "clever coder";var person = {    name : "foocoder",    hello : function(sth){        var sayhello = function(sth) {            console.log(this.name + " says " + sth);        };        sayhello(sth);    }}person.hello("hello world");  //clever coder says hello world
 //7var name = "clever coder";var person = {    name : "foocoder",    hello : function(sth){        var that = this;        var sayhello = function(sth) {            console.log(that.name + " says " + sth);        };        sayhello(sth);    }}person.hello("hello world");  //foocoder says hello world
//8var name="window";var Bob={    name:"Bob",    showName:function(){        alert(this.name);    }};var Tom={    name:"Tom",    showName:function(){        var fun=Bob.showName;        fun();    }}Tom.showName();  //window
//9var name="Bob";var nameObj={    name:"Tom",    showName:function(){        alert(this.name);    },    waitShowName:function(){        !function(__callback){            __callback();        }(this.showName);    }}nameObj.waitShowName(); //Bob
 var name="Bob"; var nameObj={     name:"Tom",     showName:function(){         alert(this.name);     },     waitShowName:function(){         var that=this;         setTimeout(function(){             that.showName();         },1000);     } };nameObj.waitShowName(); //Tom
				
时间: 2024-07-28 21:55:24

说出this的三个应用的相关文章

JS弹出对话框的三种实现方式的意义

最近开始学习JavaScript,最开始讲的就是alert().confirm()和prompt()三种JS弹出对话框.三种弹出对话框分别是警告.确认和提示消息. 第一种警告消息框 (alert)     alert 方法有一个参数,即希望对用户显示的文本字符串.该字符串不是 HTML 格式.该消息框提供了一个"确定"按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说,用户必须先关闭该消息框然后才能继续进行操作. <script> alert("Hello

写出MVC的三个字母分别代表什么含义

M:model (模型):javaBean.srping.hibernate.mybatis V:view(视图) :jsp.html.freemaker C:controller(控制器):servlet .struts.springmvc 写出MVC的三个字母分别代表什么含义

【c语言】给出三角形的三边长,求三角形面积

// 给出三角形的三边长,求三角形面积 // area = sqrt( s * ( s - a ) * ( s - b ) * ( s - c ) ) // s = ( a + b + c) / 2 #include <stdio.h> #include <math.h> int main() { int a,b,c; double s,area; printf("请输入三角形三个边长:"); scanf("%d%d%d",&a,&a

JS弹出对话框的三种方式

JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 <html> <head> <title>编写html页面</title> <script language="javascript"> //JavaScript脚本标注 alert("15");//在页面上弹出 &

浅入浅出EmguCv(三)EmguCv打开指定视频

打开视频的思路跟打开图片的思路是一样的,只不过视频是由一帧帧图片组成,因此,打开视频的处理程序有一个连续的获取图片并逐帧显示的处理过程.GUI同<浅入浅出EmguCv(二)EmguCv打开指定图片>一样,只不过处理程序编程如下所示: 1 /// <summary> 2 /// 点击按钮打开指定图片 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <pa

LeetCode第十六题-找出数组中三数之和最接近目标值的答案

3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nums=[-1, 2, 1, -4], 目标值:target = 1. 最接近目标值的答案是2 (-1 + 2 + 1 = 2). 解法一: 与上一道题类似,这次要求的是三数之和与目标值的差值最小值,可以定义一个变量来记录这个差值 思路就是想先定义一个最接近的值默认取前三个数的合,然后将数组排序后 小

LeetCode 16 3Sum Closest 找出最接近指定target的三个数的和

题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-

聊下最近出的一些wannacry勒索病毒防御工具

1. 瑞星之剑. 只能怪自己消息太过闭塞, 这工具出了两三天了, 好像瑞星还在大肆宣传其防御效果.于是好奇下载下来分析下.界面如下: 就是一个简单的EXE文件, 运行会释放一个dll和两个驱动文件.然后将驱动文件拖入IDA分析. 实现如下: 使用minifilter过滤文件io操作.IRP_MJ_CRATE,IRP_MJ_WRITE, IRP_MJ_SET_INFORMATION. 当有进程试图去写入 或者重命名 瑞星事先在指定目录释放的一些陷阱文件(jpg txt doc)则就会弹窗提示发现敲

第三章 基本概念 --《Javascript高级程序设计》

一.语法 1.区分大小写 ECMAScript 中的一切(变量.函数名和操作符)都区分大小写. 2.标识符 所谓标识符,就是指变量.函数.属性的名字,或者函数的参数. 标识符可以是按照下列格式规则组合起来的一或多个字符: 第一个字符必须是一个字母.下划线(_)或一个美元符号($): 其他字符可以是字母.下划线.美元符号或数字. 按照惯例,ECMAScript 标识符采用驼峰大小写格式,也就是第一个字母小写,剩下的每个单词的首字母大写,例如: firstSecond    myCar     do