-1

I am a beginner in JS and I just started learning about functions today and the W3C example

var myObject = {
    firstName:"John",
    lastName: "Doe",
    fullName: function () {
        return this.firstName + " " + this.lastName;
    }
}
myObject.fullName();         // Will return "John Doe"

So W3C says "The thing called this, is the object that "owns" the JavaScript code. "

Can someone explains how this is an object and the concept of this?

Thanks

Mitesh Pathak
  • 1,306
  • 10
  • 14
jayc
  • 63
  • 6

2 Answers2

0

probably asked many many times but:

The 'this' in this.firstName is the object which you're currently 'working on', since the funcion will be called from some object, you can use a generic 'this' to refer to whichever object is calling it.

If it sounds too technical use this as an example:

You're creating an object called var, which has several attributes (firstName, lastName) and an annonymous function (a function with no name) that will return a concatenation of the first and last name. 'this' is the scope which you're in, meaning the object itself

Maor
  • 118
  • 9
0

WoW. Very top level question. I'll try to give you a very conceptual top-level answer, and after having read it, go read this: http://www.w3schools.com/js/js_objects.asp And after that you should search for "object oriented programming" tutorials that explain everything in details from scratch.

An object is called like this because its the best way of abstraction to represent it for a human: an object in the programming world can be ANYTHING, just like an object can be anything in the real world. The developer choose what the object really is when defining it. The way it declares the object will define what it is "virtually", which make it more understandable and workable for a human brain. In your explamlle, the object is Jonh Doe. For the machine, it is just another variable, that can have many values.

Thomas G
  • 9,222
  • 7
  • 24
  • 36