0

I have this piece of code

var temp = "text " + this.link + " text";

$(data.query.results.item).each(function () {
    alert(temp);
});

When alerted it returns "text undefined text"

But if i do this

$(data.query.results.item).each(function () {
    alert("text " + this.link + " text");
});

It returns the correct value

Why is this happening and how can i get it to work?

Thanks

4
  • 2
    What is the scope of this in your first example? Commented May 21, 2014 at 7:21
  • 1
    what is this.link in first code snippet ? Commented May 21, 2014 at 7:23
  • if temp is a global variable then this would refer to the window object. Commented May 21, 2014 at 7:24
  • this.link refers to data.query.results.item Commented May 21, 2014 at 7:26

4 Answers 4

1

this.link is not accessible from the outside of the each loop. You can get access this context only from teh each scope. If you want to define some "template" ouside of the loop, you can try with:

var temp = "text %link% text";

$(data.query.results.item).each(function () {
    alert(temp.replace('%link%', this.link));
});
Sign up to request clarification or add additional context in comments.

4 Comments

what if that .each has been wrapped around another one .each ..?
So what if i wante to put in the template more, like var temp = "text %link% text %image% text %title%". How can i replace all these without declaring this.link, this.image, this.title inside each.
@DimitrisBorbotsialos You have to bound key %...% with value this...., so for text %link% text %image% text %title% it should be temp.replace('%link%', this.link).replace('%image%', this.image).replace('%title%', this.title)
Thanks for your time. The thing is that i tried to use John Resig's micro template engine for this purpose. I've posted the original question but no answer yet here: stackoverflow.com/questions/23757090/… If you have the time take a look of what i try to accomplish. Thanks again
1

this refer to the context/scope where it is executed, your this is executed outside of each, that is why it is undefined -

You can try something like this -

function getText(item){
  var temp = "text " + item.link + " text";
}

$(data.query.results.item).each(function () {
    alert(getText(this));
});

3 Comments

I would go further and pass only this.link to the getText function.
@hsz sure, but item may have another properties that OP might want to include in text (in future)
On the other hand - it cannot be used for other kind of item. We can only speculate. ;-)
0

Because this always refers to the current object.

var temp = "text " + this.link + " text";

Here this is undefined that's why it's showing that message.

Comments

0

Why not use each method in this way!

$.each(data.query.results.item, function( index, value ) {
    alert( "text " + value.link );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.