/    Sign up×
Community /Pin to ProfileBookmark

Newbie // this, object,helper function and accessing prototype functions

Good day,

I am working with objects, and am confused as to the use of this, and the use of helper functions

Question 1 –
I was hoping someone could explain why the following code works if I set intermediate variables for this.bilex.lexi and this.bilex.tableLex. If I remove them and call this.bilex.lexi and this.bilex.tableLex from whithin the helper function (createListe), the program crashes. Is there an alternate way of doing this? Although the program works, it comes at a cost of readability.

Question 2 – from within the helper function (createListe)
, I am trying to acess the this.findLocation(word), which is a prototype function defined elsewhere. Tracing the code with [url]http://codeboot.org/[/url], I can see that if I call the this.findLocation(word) within the prototype.createBilexique but not within the helper, it does call it. However, from within the helper function the call to this.findLocation(word) makes the program crashes.

What am I not understanding here?

Thanks,

Chris

[CODE]
//constructor is separate part not listed here
Bilexique.prototype.createBilexique = function (){

var index = 0;
[B] var bilexLexi= this.bilex.lexi
var tableLex = this.bilex.tableLex[/B]
var tabToReturn = []

[B]var createListe = function [/B](word, counter, index){
var tabTemp = [];
for (i=0;i<counter;i++){
var desti = [B]this.findLocation(word);[/B]
this.insert(tableLex[index], desti);
index++
};
if (tabTemp.length > this.n) temp.slice(0, this.n);
return tabTemp;
}

for (var i=0;i<tableLex.length;i++){
tabToReturn.push(bilexLexi[i][0]);//mot
tabToReturn[i][1].push(createListe (bilexLexi[i][0], bilexLexi[i][1], index))//table des traductions
}
}; [/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@iBeZiDec 16.2014 — All functions have their own "context", and the context of your prototype function and the prototype of the createListe function are different, "this" has a different value depending on the context of the function which is decided by how the function was originally called.

Inside createBilexique the context is that of the object instance you're calling the function on because you use that object to call the function, take the following example

[code=php]
var MyObject = function() {

this.aFunction = function() {
console.log(this);
}

var bFunction = function() {
console.log(this);
}

bFunction();
};

MyObject.prototype.protoFunction = function() {
console.log(this);
};

var anObject = new MyObject();

anObject.aFunction();
anObject.protoFunction();
[/code]


Right, now the first function that will run is bFunction, this is invoked [b]without[/b] a context, being called simply with bFunction(), because you haven't specified a context "this" defaults to the global "window" object.

The second function, aFunction, is being called using the object "anObject", because you're using that object to invoke the function the context will be that object, so "this" is equal to "anObject".

We're then using the prototype to create and run the function "protoFunction", even though this function is inside the prototype the context is [i]still[/i] "anObject" because we're using that to call the function, so "this" is again equal to "anObject".

Now if you have a function inside a prototype function that you want to have access to the same "context" as the main function you can do so by assigning it to another variable then using that instead, for example we could assign "this" to a variable called "that" then use that variable instead.

<i>
</i>//constructor is separate part not listed here
Bilexique.prototype.createBilexique = function (){

<i> </i>var index = 0;
<i> </i>var bilexLexi= this.bilex.lexi
<i> </i>var tableLex = this.bilex.tableLex
<i> </i>var tabToReturn = []

<i> </i>//store our context in a variable called "that"
<i> </i>var that = this;

<i> </i>var createListe = function (word, counter, index){
<i> </i> var tabTemp = [];
<i> </i> for (i=0;i&lt;counter;i++){

<i> </i> //since our context here is different we need to use "that" to access the createBilexique context
<i> </i> var desti = that.findLocation(word);

<i> </i> this.insert(tableLex[index], desti);
<i> </i> index++
<i> </i> };
<i> </i> if (tabTemp.length &gt; this.n) temp.slice(0, this.n);
<i> </i> return tabTemp;
<i> </i>}

<i> </i>for (var i=0;i&lt;tableLex.length;i++){
<i> </i> tabToReturn.push(bilexLexi[i][0]);//mot
<i> </i> tabToReturn[i][1].push(createListe (bilexLexi[i][0], bilexLexi[i][1], index))//table des traductions
<i> </i>}
};


See here for more information on "this" contexts
×

Success!

Help @regform spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...