Why reading undeclared variable gives reference error but undeclared object property returns undefined in javascript?

In below code, if I print this.property2 on console, JS engine prints undefined, if I replace the statement to print only property2 compiler
throws a reference error
if I declare the variable property2 then the compiler will return undefined.

function foo() {
  var property;
  console.log(this.property2)
  console.log(this.property2 === property2)
  console.log(property2)
}

foo.property="property value";
var property2; // modified to resolve reference error
foo();
console.log(foo.property);

As I have shown in the code this.property2 is equal to property2 so why javascript behaves differently for the same variable.

Coming to my question are both variables different? (this.property2 and property2) if they are different why the comparison returns true?
and if they are not different then why I have to declare property2 to get the return value and this.property2 returns values without
declaration.

am I missing anything?


Source: stackoverflow-javascript

Does anyone know why the output is "Obama" not "Clinton"?

function setName(obj){
    obj.name = "Obama";
    obj = {name:"Clinton"};
}
var president = {name:"Bush"};
setName(president);
console.log(president.name);

When I change “obj.name = “Obama”;” to “this.name = “Obama”;”

function setName(obj){
    this.name = "Obama";
    obj = {name:"Clinton"};
}
var president = {name:"Bush"};
setName(president);
console.log(president.name);

The output becomes “Bush”.


Source: stackoverflow-javascript

Difference between using .bind(this) and wrapping inside function class with JSlint ES5

Sorry if i ask similar questions, I am trying to better understand JS with JSlint as well as how this/.bind(this) ties into it.

I have 3 examples below which i am working with. I have figured out why example 1 and 3 behave the way are.

For example 2, i cannot figure out why JSlint complains about the “.” in .bind(this).

For example 3, i wonder if there is a way to optimise it?

The code can be copied and pasted into http://www.jslint.com/ to see the results in action.

Example 1: No JSlint warnings.

The issue is that “this” does not refer to window or an object which is why it’s undefined, which is why it cannot set “this.c”. Pretty self explanatory.

/*jslint devel:true, browser:true, this:true*/
/*global $, window*/

$(function () {
    "use strict";
    window.myApp = (function () {
        this.c = "c";

        this.main = function () {
            console.log("MAIN ", this.c);
        };
        this.main();
    }());
}());

Example 2: JSlint complains about the “.” in .bind(this), but otherwise the code is working

[jslint] Unexpected ‘.’. (unexpected_a)

/*jslint devel:true, browser:true, this:true*/
/*global $, window*/

$(function () {
    "use strict";
    window.myApp = (function () {
        this.b = 'b';

        this.main = function () {
            console.log('MAIN ', this.b);
        };
        this.main();
    }.bind(this)());
}.bind(this));

example 3: Wrapping inside function class App

In this case, JSlint does not show any errors/warnings, it is executed without an issue and all object are accessible.

/*jslint devel:true, browser:true, this:true*/
/*global $, window*/

$(function () {
    "use strict";
    window.myApp = (function () {
        function App() {
            this.a = "a";

            this.main = function () {
                console.log("MAIN ", this.a);
            };
        }
        var t = new App();
        t.main();
    }());
}());


Source: stackoverflow-javascript