/    Sign up×
Community /Pin to ProfileBookmark

Methods and diff between local scope and member variables???

“`
function Person(name, age)
{
this.name = name;
this.age = age;

this.Print = function()
{
document.write(this.name + ” ” + this.age + “<br>”);
}
}
“`

So I’m trying to understand oop in javascript. I read that functions are by default objects. This means that the method **this.Print** is actually an independent object from the Person but inherits it’s data, right? Actually, from a video, I saw that nested functions actually are lambdas living in the scope of the function above them. But still, that does not explain why **this.Print** can access member variables because:

What is the difference between local scope variables and members? For example:

“`
function Person(name, age)
{
var name = name;
var age = age;

this.name = name;
this.age = age;

this.Print = function()
{
document.write(this.name + ” ” + this.age + “<br>”);
}
}
“`

I understand that **this** is the reference of the actual object of the function itself, but where does the **this** variable in this case live? Does it live in the local scope of the Person?

### The “`this.Print“` has its own “`this“` reference which inherits the “`this.name“` and “`this.age“` from **Person** or the “`this“` is the same for the Print function and the Person?

But if the “`this“` reference is indeed the same for both the **Person** and **this.Print** then the **this.Print** is not an actual object.

I know its too many questions, but I want to clear all this in order to know exactly what I am doing.

One last thing. Can someone tell me what exactly is happening in the code bellow and why am I getting that output?

“`
function Person(name, age)
{
this.name = name;
this.age = age;

this.Print = function()
{
document.write(this.name + ” ” + this.age + “<br>”);
}

return this.Print;
}

var p = new Person(“Nick”, 20);
p();

var myPrint = Person(“Nick”, 20);
myPrint();
“`

**Output**:

“`
Nick undefined
Nick 20
“`

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@babaliarisauthorJul 31.2019 — I tried the example bellow:
``<i>
</i> function Person(name, age)
{
this.name = name;
this.age = age;

this.Print = function()
{
document.write(this.name + " " + this.age + "&lt;br&gt;");
return this;
}
}

var p = new Person("Nick", 20);

var ThIs = p.Print();

document.write(ThIs === p);<i>
</i>
`</CODE>

<STRONG>**Output**</STRONG>:
<CODE>
`<i>
</i>Nick 20
true <i>
</i>
``


well, this proves that indeed Methods are not actual objects since their **this** keyword is the same as the **p** object which I created from the _Person._ .
Copy linkTweet thisAlerts:
@AppsmavenAug 01.2019 — Hi,

A local variable is the variable you declare in a function.

class Program

{

static void Main()

{

// This is a local variable. Its lifespan

// is determined by lexical scope.

Foo foo;

}

}

A member variable is the variable you declare in a class definiton.

class Foo

{

// This is a member variable - a new instance

// of this variable will be created for each

// new instance of Foo. The lifespan of this

// variable is equal to the lifespan of "this"

// instance of Foo.

int bar;

}
Copy linkTweet thisAlerts:
@daveyerwinAug 01.2019 — @babaliaris#1607004

In JavaScript, almost "everything" is an object.

Booleans can be objects (if defined with the new keyword)

Numbers can be objects (if defined with the new keyword)

Strings can be objects (if defined with the new keyword)

Dates are always objects

Maths are always objects

Regular expressions are always objects

Arrays are always objects

Functions are always objects

Objects are always objects

All JavaScript values, except primitives, are objects.
``<i>
</i>&lt;script&gt;
'use strict'
var printIt = function ()
{
document.write("hiyas from window.printIt&lt;br&gt;");
return this;
}


var Print = function(){
this.printIt = function ()
{
document.write("hiyas from Print.printIt&lt;br&gt;");
return this;
}
}

var p = new Print();
var that = p.printIt();
document.write(that === p, "&lt;br&gt;")
document.write(typeof p.printIt, "&lt;br&gt;")
that = window.printIt();
document.write(that === window, "&lt;br&gt;")
document.write(typeof printIt, "&lt;br&gt;")

&lt;/script&gt;<i>
</i>
``


As you can see from the above code,

var printIt is a method of the window object.

this.printIt is a method of the Print oblect.

The "new" keyword makes the "this" variable point

to the newly created object.

This proves that indeed Methods are actual objects

that inherit their scope ( "this keyword" ) fom

their Parent object.
Copy linkTweet thisAlerts:
@daveyerwinAug 02.2019 — @babaliaris#1607004 said ...

> ``<i>
&gt; </i>function Person(name, age){
<i>&gt; </i> var name = name;
<i>&gt; </i> var age = age;
<i>&gt; </i> this.name = name;
<i>&gt; </i> this.age = age;
<i>&gt; </i> this.Print = function(){
<i>&gt; </i> document.write(this.name + " " + this.age + "&lt;br&gt;");
<i>&gt; </i> }
<i>&gt; </i>}<i>
&gt; </i>
`</CODE>
<i>&gt; </i>I understand that this is the reference of the actual object of the function itself, but where does the this variable in this case live? Does it live in the local scope of the Person?</QUOTE>
<i>&gt;</i>
<CODE>
`<i>
</i>&lt;script&gt;
//Person is a function
//scope (this) is inherited
//from parent object

function Person(name, age){
var name = name;
var age = age;
this.name = name;
this.age = age;
this.Print = function(){
document.write(this.name + " " + this.age + "&lt;br&gt;");
}
alert(this)
}
Person();//alerts [object Window]
//so 'this' points to parent object

//it can be a constructor
var Joe = new Person;//alerts [object Object]
// because the new keyword points 'this' to
//the newly created Person Object and it
//lives in the var Joe

&lt;/script&gt;<i>

</i>
``
Copy linkTweet thisAlerts:
@babaliarisauthorAug 02.2019 — @DaveyErwin#1607055 thank you! That was the right answer I was looking for!!!
×

Success!

Help @babaliaris 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 6.15,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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