0

Hi i'm trying to get the refence to the attribute name in that declaration but when i print this.name inside fun attribute (function definition), of my object it doesn't work, instead when i use this.name in this.img attribute, it works.

why?

here's my code

    var submenu = new function(){

        this.name =  museum[i].name,
        this.title = 'It will merge row',

        this.img =img/this.name + '.png', //it work

        this.fun =  function (data) {
            console.log(this.name); //it doesn't work
        }
  };

it prints undeclared....

PMerlet
  • 2,568
  • 4
  • 23
  • 39
userfi
  • 175
  • 1
  • 1
  • 15
  • are you sure that this `this.img =img/this.name + '.png', //it work` works as is or it is a typo? – Bellash Sep 28 '15 at 13:24
  • 1
    How exactly are you calling `this.fun`? What is the behavior you see? You should definitely not use the comma operator here. Use semicolons to terminate he assignments. – Felix Kling Sep 28 '15 at 13:25

5 Answers5

2

try storing the scoped this into a variable

var submenu = new function(){
    var that = this;
    that.name =  museum[i].name,
    that.title = 'It will merge row',

    that.img ="img/"+that.name + '.png', //it work



     that.fun =  function (data) {
               console.log(that.name); //it doesn't work

     }
 };

EDIT

This error happens because "this" is a scoped keyword and a little change in the scope will affect the keyword content. So, you store the keyword in the scope you want to work and use the variable instead.

Bellash
  • 7,560
  • 6
  • 53
  • 86
Filipe Merker
  • 2,428
  • 1
  • 25
  • 41
2

Because the this keywords has some funny behaviours, and it changes depending on how you call your functions!

var submenu = new function(){
  var self = this;
  //in here, `this` refers to the `submenu` function
  self.name =  museum[i].name;
  self.title = 'It will merge row';
  self.img = 'img/'+ self.name + '.png';

  self.fun =  function (data) {
    console.log(self.name);
    //but in here `this` refers to the `fun` function
  }
};
Community
  • 1
  • 1
Marcelo
  • 4,580
  • 7
  • 29
  • 46
0

Every function invocation has both a scope and a context associated with it. Context is always the value of the this keyword which is a reference to the object that “owns” the currently executing code. try assingning the context of your submenu fuction to a variable and use that variable in the inner function

var submenu = new function(){
var that = this;
that.name =  museum[i].name,
that.title = 'It will merge row',

that.img ="img/"+that.name + '.png',

that.fun =  function (data) {
           console.log(that.name); 

   }
}
Overmachine
  • 1,723
  • 3
  • 15
  • 27
0

You don't have the this.name var inside the this.fun function! Please create a local copy as bellow

   var submenu = new function(){

    this.name =  museum[i].name,
    this.title = 'It will merge row',

    this.img ='img/'+this.name + '.png', //it work

   var clone=this;

     this.fun =  function (data) {
               console.log(clone.name); //it doesn't work

      }
 };
Bellash
  • 7,560
  • 6
  • 53
  • 86
0

Write your object and use prototyping:

var submenu = function(){
    this.name =  museum[i].name;
    this.title = 'It will merge row';
    this.img ='img/' + this.name + '.png';
};
submenu.prototype.fun =  function (data) {
   console.log(this.name); 
};
TwilightTitus
  • 190
  • 1
  • 9
  • How exactly does this solve the problem? – Felix Kling Sep 28 '15 at 13:30
  • It's true that this works, but then again, so would `submenu.func()`, which would be a much simpler solution since it doesn't require changes to the code the OP posted. The OP's code already works if called correctly. The issue is that they are not calling it correctly. All you do is restructure the code but the issue will stay the same. – Felix Kling Sep 28 '15 at 14:55