`
yxylwt
  • 浏览: 14061 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

关于this 和 var

阅读更多
  很多人都觉得在javascript声明一个变量,加var和不加var没有什么区别,实际上是一个错误的观点,如果在函数外面,也就是说在window区域加不加var确实是一样,因为都会是全局变量的效果,而如果在函数内部,加var就是局部变量,不加是全局变量。
function test(){  
  var1 = 2;
  alert(var1);     
}  
test();  
alert(var1);
这样可以显示为2
function test(){  
  var var1 = 2;
  alert(var1);     
}  
test();  
alert(var1);
则为变量未定义错误

this多用于函数内部,它永远指向调用他的那个对象,看看下面的例子就明白了

var test={
   a:"test",
   msg:function(){
     a="1234";
     alert(this.a);
     var msg1= function(){
alert(this.a);
     }
     msg1();
   }
}
test.msg(); 这样会分别显示 "test" ,"1234".

但this也不是这么简单,在看看下面的例子
function test1()  
{  
  this.var01 = "test";
}

function test2()  
{
  alert(var01);
}

var a= new test1();
test1();
test2();
alert(a.var01);
都可以得到"test".
可以发现var01变量既是一个全局变量,又是test1内的成员变量。也就是说,如果在函数内部用this定义的变量,如果这个函数你不是直接来运行,而是作为一个类来new的话,虽然函数也要执行一遍,但里面的this定义的 变量是作为类的成员变量来定义的,也就是一个局部变量,上面的测试代码,如果去掉test1();  test2()运行就是未定义变量错误了。

说到变量,最后提一下js变量无类型的说法,这个说法本身没有错,但js变量无类型,这话很模糊,有的人会理解成js 里面没有数据类型。这显然是错误的,js 里面也有数字,字符,布尔,类等数据类型。说变量无类型,指的是刚定义一个变量的时候,这个变量是没有类型的,当你给它赋值的时候,值是什么类型,那这个变量就是什么类型了
分享到:
评论
1 楼 peacock 2008-10-14  
学习了,感谢分享

相关推荐

    微信小程序开发中var that =this的用法详解

    在微信小程序开发中,var that =this的声明很常见。举个例子,代码如下! 示例代码1 //index.js Page({ data: { toastHidden: true, }, loadData: function () { var that = this//这里声明了that;将this...

    R_var_R语言var代码_

    此代码可以用于R语言var模型(This code can be used for R language VAR model)

    浅析JavaScript中var that=this

    在阅读别人的代码时,发现别人写的代码中有这么一句:var that = this;,这代表什么意思呢?经过一番查阅,才明白是这么回事。 在JavaScript中,this代表的是当前对象。 var that=this就是将当前的this对象复制一份...

    var r = this.onTimeout(this.duration, this);解决方法

    这版本的AjaxPro.net框架集解决了出现this.onTimeout is not a function的错误,也是经常出现的"缺少对象的错误",同时包括AjaxPro.dll,AjaxPro.2.dll,AjaxPro.JSON.dll, AjaxPro.JSON.2.dll

    Multiple Time Series Modeling Using the SAS VARMAX Procedure pdf

    This instability can be compensated for by application of Bayesian methods, which are also incorporated in PROC VARMAX. Volatility modeling has now become a standard part of time series modeling, ...

    A Varactor Tuned RF Filter

    paper discusses the effects of the varactor series resistance and the electrical length of the distributed resonator on the overall resonator quality factor and filter insertion loss. The IIP3 (input ...

    微信小程序中为什么使用var that=this

    主要介绍了小程序中为什么使用var that=this的相关知识,本文通过示例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

    微信小程序开发中 var that =this的用法

    在微信小程序开发中,var that =this的声明很常见。举个例子,代码如下! 示例代码1 1 //index.js 2 Page({ 3 4 data: { 5 toastHidden: true, 6 }, 7 8 loadData: function () { 9 var that = this//这里...

    Data Driven Mean-CVaR Portfolio Optimization Model.pdf

    This paper studies the out-of-sample performance of the data driven Mean-CVaR portfolio optimization(DDMC) model, in which the historical data of the stock returns are regarded as the realized returns...

    jquery需要的所有js文件

    a(b).parents().andSelf().filter(function(){return a.curCSS(this,"visibility")==="hidden"||a.expr.filters.hidden(this)}).length}function c(b,c){var e=b.nodeName.toLowerCase();if("area"===e){var f=b....

    the vark questionnaire

    this document test the learning style,you can caculate the learning style by yourself.

    F7.rar_SVC simulink_matlab svc mvar_paper_pdf_svc

    This paper investigates the effects of Static Var Compensator (SVC) on voltage stability of a power system. This paper will discuss and demonstrate how SVC has successfully been applied to power ...

    基于js中this和event 的区别(详解)

    今天在看javascript入门经典-事件一章中看到了 this 和 event 两种传参形式。因为作为一个初级的前端开发人员平时只用过 this传参,so很想弄清楚,this和event的区别是什么,什么情况下用什么比较合适。 onclick = ...

    javascript中的self和this用法小结

    一、 起因 那天用到prototype.js于是...代码如下:var Class = { create: function() { return function() { this.initialize.apply(this , arguments); } } } // Class使用方法如下var A = Class.create(); A. prototy

    var.rar_About Language_grewi89_industrialflb_var

    This program is about how to calculate the VaR, written in R language.

    【JavaScript源代码】NodeJS和浏览器中this关键字的不同之处.docx

    NodeJS和浏览器中this关键字的不同之处  学习过JavaScript的人肯定清楚 处在不同环境下this的指向问题。那么看下面的代码 var type = 1 function toWhere(){ this.type = 2; } toWhere(); console.log(type)...

    js中this对象用法分析

    this对象是在函数运行时,基于函数的执行环境绑定的。 其实这句话的本质就是,谁调用了函数,this就指向谁 具体的来说,通常有以下几种情况: ...var b = { getThis:function(){ console.log(this

    js的日历时间控件

    setDate:function(a){if(a){while(this.element.firstChild){this.element.removeChild(this.element.firstChild)}this.calendarCont=this.drawCalendar(this.element,a)}},drawCalendar:function(a,d){var l=a;...

Global site tag (gtag.js) - Google Analytics