javascript中this
下面都是个人理解以及查找的网上的资料,如有不对的地方请指正
This
this 始终指向调用它的对象 ,都没有对象调用时就指向window
另外就是this一般都是在function中,当不在function中的时候 一定是指向window的.
var a ='a';
alert(this.a); //出来的是a
alert(this.b); //undefined 因为还没定义
var b= 'b';
alert(this.b); //出来的是b 所有的全局变量都是window的属性
当在function中的时候,如果没有对象调用它还是指向window的
var a ='a';
function test(){
alert(this.a)
}
test();
有人觉得this是根据上下文来确定的,反正我不明白这是什么意思 只要抓住一点 有没有对象调用它就好了,不管这个this隐藏的有深
var a ='a';
function test(){
(function(){
(function(){
(function(){
alert(this.a); //这个this隐藏的够深了吧 但是没有对象调用 还是指向window
})();
})();
})()
}
test();
在function中 当有对象调用这个function的时候 this就指向这个对象了......
var vv = "wo bu shi vv";
function text(){
alert(this.vv)
};
var obj = {
vv : "vv",
say :text
};
text(); //可以看到 没有对象调用 指向的是window
obj.say();//say引用的text 有对象调用(obj) this指向的是obj
在js中有一个神奇的方法,他就是call,apply他可以改变this 的指向;
var text = {
name : "we are good",
say : function(){
alert(this.name);
}
}
var pest ={
name : "we are pest"
}//可以看到pest没有say这个方法
text.say.call(pest);//可以看到alert出来的是we are pest
text.say.apply(pest);
还有一中情况就是类了, js中的类就是function吗,当new一个function的时候就会生成一个对象(也不一定啊 当在某些情况下还会是function 如构造类 现在我说的是基本的) this 就指向这个对象了
function perseon(name){
this.name = name;
this.say = function(){
alert(this.name)
}
}
var wt = new perseon('wt');
wt.say(); //指向的wt
var taozi = new perseon('taozi');
taozi.say();//指向的taozi
以上都是针对js语言来说明的 所说的对象也是原生(Native)对象
另外还有宿主(Host)对象 如dom元素
但是原理还是一样 如果是元素的方法调用的函数 函数里面的th
相关文档:
<script language="JavaScript" type="text/javascript">
<!--
// 说明:用 JavaScript 实现网页图片等比例缩放
// 整理:http://www.CodeBit.cn
function DrawImage(ImgD,FitWidth,FitHei ......
用Javascript可以实现对GridView中某一行选择,而无需进行页面的刷新。
首先要在GridView的onrowdatabound的event handler中为GridView中的每一行onclick绑定event handler (Javascript)。假如GridView代码如下:
<asp:GridView runat="server" id="GridViewCategory" Aut ......
Flex 与 JavaScript 交互,主要依靠Flex的ExternalInterface,其提供了addCallBack和call方法.
下面的例子将演示Flex调用javascript,和javascript调用Flex。
js 代码
-------------------------------------------------------------------------------------------------------------
function hello(param) {
......
Several programming languages implement a sprintf function, to output a formatted string. It originated from the C programming language, printf function. Its a string manipulation function.
This is limited sprintf Javascript implementation. Function returns a string formatted by the usual printf co ......
这里考虑的是.net服务器控件checkbox或checkboxList;
假设页面如下,chkDepart是部门,chkPeople是所属部门的人员
<div style="text-align: center" mce_style="text-align: center" width="95%" class="tab">
<asp:DataList ID="DataList1" ......