1

I am just starting to learn Javascript and was experimenting with functions. I wrote the following thinking that it would work. Is something like this possible or is it just wrong?

function foo() {
   console.log(this.language);
}

foo.language = "English";

foo();
RobertJoseph
  • 7,968
  • 12
  • 68
  • 113
  • 'this' will only exist when you have created an instance with the 'new' keyword. – html_programmer Feb 13 '16 at 13:25
  • @Nonemoticoner - Pretty sure the OP did run it, found it doesn't work, and that's what prompted the question. – nnnnnn Feb 13 '16 at 13:32
  • 1
    Um... yes, of course I ran it, debugged it, researched it and then eventually asked this question. `this` in javascript obviously doesn't work like other OO languages. Thanks for the downvote @Nonemoticoner. – RobertJoseph Feb 13 '16 at 13:36
  • 1
    @RobertJoseph: I don't know any OO language in which `this` refers to the function object itself… but yeah you're right, [`this` is a bit different in JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) – Bergi Feb 13 '16 at 13:46
  • 1
    You are correct that `this` in Javascript is a bit different to other languages. Although the other languages I'm familiar with (including Java and C#) don't let you assign properties to a function and use them like that either. In JS the value of `this` depends on how a function is called, but it's almost never the function itself. I used to recommend [the MDN page about `this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this), because it used to explain it fairly clearly, but I think the latest version is somewhat confusing. Still, it might help you. – nnnnnn Feb 13 '16 at 13:48
  • Thank you @nnnnnn! It is funny that the first sentence in that article is, "A function's this keyword behaves a little differently in JavaScript compared to other languages." ;-) – RobertJoseph Feb 13 '16 at 13:50

4 Answers4

2

It is not possible, because to gain access to this as the current object, you'll need to instantiate the (constructor) function, which is what invoking new will do for you. Accessing this for a reference to the current instance is not only the case in Javascript though; in Java, this also isn't accessible from static context.

What you're looking for is something like:

function Foo(language) {
   this.language = language; 

}
Foo.prototype.log = function(){
   console.log(this.language); 
}    
var bar = new Foo("English"); 
bar.log(); 
html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • [Don't create prototype methods in the constructor!](http://stackoverflow.com/questions/28255957/assigning-prototype-methods-inside-the-constructor-function-why-not) – Bergi Feb 13 '16 at 13:44
1

You can make a foo object (an instance of foo) using the new keyword. Then set the language property of that instance. If foo has a function to output its language property, then you can invoke that function on your instance to see its language.

function foo() {
    this.outputLanguage = function() {
        console.log(this.language);
    }
}

var f1 = new foo();
f1.language = "English";

var f2 = new foo();
f2.language = "French";

f1.outputLanguage();
f2.outputLanguage();

In this example, we make 2 foo objects, and assign each a different language.

The foo() function is a constructor function. We usually pass the initial values of the instance variables to it, like this:

function foo(language) {
    this.language = language;
    this.outputLanguage = function() {
        console.log(this.language);
    }
}

var f1 = new foo("English");
var f2 = new foo("French");

f1.outputLanguage();
f2.outputLanguage();
whistling_marmot
  • 3,561
  • 3
  • 25
  • 39
1

Javascript only supports prototypal Inheritance. So to assign a property (in your case language is the property) to an Object (in your case foo) you need to use constructor. And by convention constructor are written Initcap.

function Foo(lang){
 this.language = lang;
}

Foo.prototype.getLanguage = function(){
  return this.language;
}

var lang1 = new Foo('english');
var lang2 = new Foo('bengali');

console.log(lang1.getLanguage()); // english
console.log(lang2.getLanguage()); // bengali
sapy
  • 8,952
  • 7
  • 49
  • 60
0

this is available on instance variables, in your case it's named function

function foo() {
   console.log(foo.language); //instead of this, use function property
}

foo.language = "English";

foo();

If you want to dive more, checkout my other answer for the same topic

Community
  • 1
  • 1
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69