JSFiddle - React, Tailwind, and code Playground

by tuanderful

JavaScript

/* Private Properties and Methods
 */

function Gadget(){
    this.foo = "foo";
    var bar = "bar";
    this.getFoo = function() {
        //what is the value of 'this' here?
        return this.foo;
    }
    this.getBar = function() {
        return bar;        
    }
}
    
var toy = new Gadget();
document.write(toy.foo + "<br/>");
document.write(toy.bar + "<br/>");         //our "private" variable
document.write(toy.getFoo + "<br/>");      //need the (), execution operand
document.write(toy.getFoo() + "<br/>");
document.write(toy.getBar() + "<br/>");

//can we use getBar as a ref and modify the value?
var temp = toy.getBar();
temp[0] = "bum";
document.write(toy.getBar() + "<br/>");