this在javascript中 情况是不同与java c++,
谁调用了 this对象所在的函数, this就指向谁
this引用的对象被 称为函数的 上下文 ,它不是由如何声明函数,而是由如何调用函数决定的.
根据函数如何被调用,同一个函数可以拥有不同的上下文
<script type="text/javascript">
//this是什么
var o1={handle:'o1'};
var o2={handle:'o2'};
var o3={handle:'o3'};
window.handle='window';
function whoAmI(){
return this.handle;
}
o1.identifyMe =whoAmI;
alert(whoAmI()); //结果 window
alert(o1.identifyMe()); //结果o1
alert(whoAmI.call(o2)); //结果o2
alert(whoAmI.apply(o3)); //结果o3
</script>
--------------------------------------------------------------------------
顶层函数是window对象的属性(方法) , 下面的test8(),就是顶层函数
<script type="text/javascript">
function test8()
{
alert(this==window)
}
test8();
</script>
上面代码执行结果为 true, test8函数被包含在一个名为window的全局对象中,test8函数是window对象的一个方法.所以this 指向window全局对象
----------------------------------------------------
<script type="text/javascript">
function test8()
{
alert(this==window)
}
</script>
<input type="button" value="test 8" id="bu4" onclick="test8()" /><br/>
上面代码执行结果为 true ,this是window对象
---------------------------------------------------
<script type="text/javascript">
function test8()
{
alert(arguments[0].id)
}
</script>
<input type="button" value="test 8" id="bu4" onclick="test8(this)" class="cla"/>
上面代码执行结果为bu4, 这里的this是 button对象
-------------------------------------------------------
<input type="button" value="test" id="bu5"/><br/>
<script type="text/javascript">
function test11()
{
var obj=document.getElementById('bu5');
obj.onclick=function(){
alert(this.id)
}
}
test11()
</script>
上面代码执行结果为bu5 , 这里的this是 button对象 ,因为obj.onclick=function(){} ,是为button注册了个匿名函数 ,这个匿名函数 是obj对象(button)的 函数.
-------------------------------------------------------
分享到:
相关推荐
WIN8仙三补丁(遇到this program has been damaged用)
解决redhat 8.2 yum报错 This system is not registered to Red Hat Subscription Management 解决redhat8安装centos8的rpm出错的问题
Oracle Solaris 8 Solaris 8 Sun Hardware Roadmap This document is a guide to -8
Remove folders from This PC except 3DObjects.
Oracle Solaris 8 Solaris 8 2/02 Installation Roadmap This document is a guide to the DVD, CDs, and documents i-8
Oracle Solaris 8 Solaris 8 HW 7/03 Installation Roadmap This document is a guide to the DVD, -8
yolov8系列--This is a GUI application that integrates YOLOv8
This manual documents the microcontroller profile of version 8 of the Arm Architecture, the Armv8-M architecture profile. For short definitions of all the Armv8 profiles,
也就是说,除了可以更改Win8.1的现有的文件夹外,还可以在Win7或Win8上添加文件夹。 This PC Tweaker支持任意数量的文件夹添加,因此无论你是放置5个还是3个,都只要动一动鼠标即能搞定。当然这家伙还有一个特别的...
In this exercise, you will implement a linear decoder (a sparse autoencoder whose output layer uses a linear activation function). You will then apply it to learn features on color images from the STL...
yolov8系列--Deepsort with yolo series. This project support
This chapter outlines the F2MC-8FX CPU and explains its configuration by example. CHAPTER 2 "MEMORY SPACE" This chapter explains the F2MC-8FX CPU memory space. CHAPTER 3 "REGISTERS" This chapter ...
If you are a newbie to either DirectX8 or C++ then this is for you! It is a simple and fully commented program which uses DirectX8 to render a spinning triangle in fullscreen mode使用DirectX8做的旋转...
This procedure for the use of mage8 AVR and EM4094 achieve ISO15693 type of card reader C procedures, reader stability, the project we have done for a long time, very good program, it is suitable for ...
libs is a software that permits me to be verified member of this trash website
Don’t hire a software developer until you read this book By 作者: K.N. Kukoyi ISBN-10 书号: 1539188191 ISBN-13 书号: 9781539188193 Edition 版本: 1 出版日期: 2017-05-26 pages 页数: 422 Use the ...
This resource has been expired.
stm8l15x adc h" @addtogroup STM8L15x StdPeriph Driver @{ Private typedef Private define Private macro Private variables Private function prototypes Private functions @...
代码 theMonths = new MakeArray(12) // load array with English ...this[4] = “May” this[5] = “June” this[6] = “July” this[7] = “August” this[8] = “September” this[9] =
VMware vSphere Hypervisor (ESXi ISO) image 2022-10-11 8.0.0 619.13 MB iso Boot your server with this image in order to ...SHA256SUM(¹): 78b8ee5613019f8d92da2b74fae674707679379803cb7b01b526747a849138c1