Javascript Cheat Sheet Page 2

ADVERTISEMENT

Funcject or Objection ?( Function == Object -> true; )
Objections are the organisms of JS. They are really just functions that are objects. Here’s a standard
build for a JS object.
function
MyObj(param1, param2)
//I am a
constructor
{
var
privateVar = param1;
this.publicVar = param2;
//the keyword this behaves differently in JS than in Java. this refers to
//the “owner” but the “owner” depends on the context. This is a story for
//another day. self = this is a JS idiom to keep track of the owner of
//the object
var
self = this;
function
privateFunction ()
{
private1 += 1;
}
this.privilegedIncrement =
function
()
{
privateFunction();
}
this.priviledgedGet = function()
{
return
privateVar;
}
}
There are a couple of things to note:
JavaScript is a WEAKLY-typed language == Variables don’t have types. They are not the variables
Gotham wants, they are variables Gotham needs.
JavaScript is also DYNAMICALLY-typed but that’s also a story for another day.
A JavaScript object is actually a function. It defines its constructor. You can see it two ways.
Every function is an object, or every object is a function. Call them objections.
There are 3 types of functions, and 2 types of data members.
Private data members are declared as local variables of the function with the keyword
var
, which are
only accessed by privileged and private functions.
Public data members are declared as
this.publicVar
Now, the real difference is privileged functions. They are declared inside the constructor and they are
the only functions that can be accessed publicly, yet access private functions and variables. Private
functions can, of course, access private data members.
Tl;dr public functions cannot access anything private. privileged functions can.
Using private members is not standard practice. Think Python. Trust your people to have common
sense.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3