Method and variable scoping of private and instance variables in JavaScript -
i have tried figure out or search on google, can find how create objects, not how functions work. if explain me how encapsulation works.
function myobject() { this.variable1 = "tst"; this.function1 = function() { //now function works. 'this' function private function ok _privatefunction1(); //here error one, cannot seem call methods within object //from. this.function2(); } this.function2 = function() { alert("look! function2!"); } function _privatefunction1() { //this work though. confused. _privatefunction2(); } function _privatefunction2() { alert("look! privatefunction1"); //this not work. alert(this.variable1); } }
i think can explain better if go in reverse order. first, define functions:
function _privatefunction1() { //this work though. confused. _privatefunction2(); } function _privatefunction2() { alert("look! privatefunction1"); //this not work. alert(this.variable1); }
this pretty normal stuff. thing that's weird appear inside function, that's fine. javascript has function scope, means variables defined inside function defined in new scope. not trample on global namespace. , functions first-class objects in javascript, means can used other data types. can nested, passed functions, returned functions, etc.
then run trouble:
function _privatefunction2() { alert("look! privatefunction1"); //this not work. alert(this.variable1); } }
functions in javascript executed in context referred this
keyword. when call function directly (i.e. this: functionname()
) context in function executes global window
object. so, inside _privatefunction2
, this.variable1
equivalent window.variable1
not meant.
you wanted refer current instance of myobject
this
refers outside of _privatefunction2
. can preserve access this
in inner scope storing reference in variable:
var _this = this; function _privatefunction2() { alert("look! privatefunction1"); //this not work. alert(_this.variable1); }
there's subtle here should notice. _privatefunction2
has access variables defined in lexical scope, why can access _this
. important later.
next up:
function _privatefunction1() { //this work though. confused. _privatefunction2(); }
this should normal-looking section you, think. there's nothing strange going on here. 1 regular function calling one. don't confused fact these nested inside myobject
. changes scope they're in, not else.
next define instance variables , methods:
this.variable1 = "tst"; this.function1 = function() { //now function works. 'this' function private function ok _privatefunction1(); //here error one, cannot seem call methods within object //from. this.function2(); } this.function2 = function() { alert("look! function2!"); }
here this
refer myobject
, assuming -- , it's important assumption -- myobject
called new
operator, this:
var obj = new myobject();
if had been called this:
var obj = myobject();
then this
refer window
object, did functions saw earlier. key takeaway value of this
not fixed. it's determined way in function called. there ways set arbitrary object explicitly.
the value of this
inside this.function1
current instance of myobject
, because used this:
var obj = new myobject(); obj.function1();
writing object.method()
sets this
object
inside method
.
so how this.function1
able call _privatefunction1()
? saw earlier when saving value of this
use inside nested function, _privatefunction1()
object defined in this.function1
's lexical scope, available use, _this
earlier.
and it's because of closure these private variables still alive long after myobject
has returned.
footnote: because failing use new
when instantiating objects breaks things spectacularly without warning, it's considered practice capitalize names of functions intend used constructor. myobject
should myobject
.
Comments
Post a Comment