Tuesday 15 June 2010

javascript - Accessing JS function variables through function instance -


I was trying to understand the behavior of variables defined in the function in different ways. So I tried to exclude some code and given the comment as given below. Please tell me if I think something is wrong. Or just if I miss anything, then add.

I want to know the function of instances ( objTempFun1 through ) as functionName.variableName and objTempFun2 ) As I can define the methods within the function to display the closure variables through function frequencies.

  function tempFun () {this.val1 = "this.var1"; Var val2 = "var var2"; // Locally scoped ware has not been added for work nor for prototype. This.getLocalVar = function () {return val2; }; This.setLocalVar = function (arg) {val2 = arg; }; } TempFun.prototype.val3 = "fun.proto.var3"; TempFun.val4 = "Fun.v4"; Var objTempFun1 = new tempFun (); Var objTempFun2 = new tempFun (); / * ------------------------------------------------ ----------------------- Variables defined as 'this.variableName' 1. Prototypes are declared on 2. All examples have different values ​​3 Can not access the function itself ------------------------------------------ ------ ----------------------- * / document.write ("& lt; br />obj1-this.var1:" + ObjTempFun1.val1 + "& lt; br / & gt;"); Document.write ("obj2-this.var1:" + objTempFun2.val1 + "
"); ObjTempFun1.val1 = "this.var1x"; ObjTempFun2.val1 = "this.var1y"; Document.write ("obj1-this.var1:" + objTempFun1.val1 + "
"); Dokyumentkwrite ( "Objl-Printloklvr:" + Objtempfunlkgetloklvr ( "+" & lt; br / & gt; "); Dokyumentkwrite (" Obj2-Printloklvr: "+ Objtempfun2kgetloklvr (" + "& lt; br / & gt; "); Objtempfunlksetloklvr (" VAR Vr2x "); Objtempfun2ksetloklvr (" VAR Vr2y "); Dokyumentkwrite (" Objl-Printloklvr: "+ Objtempfunlkgetloklvr (" + "& lt; br /> "); Document.write (" obj2-printLocalVar: "+ objTempFun2.getLocalVar (" + "
" / "------------ ------------------------------------ -------------- --------- Variable defined as' functionName.prototype.variablename`. 1. Announced on prototype 2. Examples of function have different values ​​3. Can not access function can not be is --------------------------------------------- ---- ---------------------- * / Dokyumentkwrite ( "Objl-Thiskprotokvr3:" + Objtempfunlkwal3 + "& lt; br / & gt;"); Dokyumentkwrite ( "Obj2-Thiskprotokvr3:" + Objtempfun2kwal3 + "& Lt; br / & gt;"); Document.write ("fun- this.proto.var3:" + tempFun.val3 + "& lt; Br /> "); ObjTempFun1.val3 =" Funny Proto V33 "; ObjTempFun2.val3 =" Funny. Proto Vr3y "Dokyumentkwrite (" Objl-Thiskprotokvr3: "+ Objtempfunlkwal3 +" & lt; br / & gt; "); Dokyumentkwrite (" Obj2-Thiskprotokvr3: "+ Objtempfun2 .val3 + "
"); / * --------------------------------- --------------- ------------------------ variable defined as' functionName.variablename` 1 Become a member of the event (or 'F' is the instance example, which is tempFun) Examples of functions (which are objTempFun1 and objTempFun2 above) 2. Examples of function can not be reached ---------- ------------------ -------------------------------- ------------ * / document.write ("Obj1-fun.var4:" + objTempFun1.val4 + "
"); Document.write ("obj2- Fun.var4: "+ objTempFun2.val4 +"
"Document.write" (fun-funny .V4: "+ tempFun.val4 +"

Search JSFiddle.

provides access to the function that created an object.

No comments:

Post a Comment