Tag Archives: JavaScript

Accessing JavaScript object properties indirectly

When it comes to accessing data in JavaScript objects there are two methods available

  • dot operator .
  • bracket notation []

Take as a example an object with the following properties:

var newObj = {
  propertyOne: "some string",
  propertyTwo: 2,
 "property Three" : "other string",
  4 : "Four"
};

Dot Operator

The dot operator is typical to method call chaining or tree style traversal.  It is useful when the name of the property is known.

newObj.propertyOne;   //  some string

Bracket Notation

The bracket notation is similar to the dot operator in the chaining process i.e. the object followed by property name.

object[propertyName][otherProperty]; // so on.

It is useful if the property name has a space in it (“property Three” for instance). But a more beneficial use is when the property’s name would vary, such as when iterating through a list.

 The bracket notation allows the use of variables as placeholders for a property’s name.

A simple example :

var placeHolder = "property Three";
newObj[placeHolder]; // yields "other string"

This is typical in a scenario  where a function is required to access or update an object’s properties. In building the function, the parameters would be generic and the dot operator would expect the exact name of a property to work. So a function signature with generic parameter names wouldn’t work:

function getProperties(ObjName, propName){
 var result = "The item you looked up is" +     ObjName.propName; 
 return result;
}

This simple example defeats the purpose of a lookup requiring only those two parameters itself, but the point is that using generic parameters means that this can be used in numerous contexts.

The dot operator used would result in undefined as the method would be looking for the specific property name.

The bracket notation, on the other hand, allows the use of variables containing the value of the property name assigned to it:

var placeholderName = propertyOne;var placeholderName = 1;

So this works well with parameter variables which would act as placeholders for a functions input.

JavaScript Arrays – Push it real good

A quick shout to JavaScript array methods. With ECMA-262 version 5 (latest version 6), came several new methods for manipulating arrays. Lets focus on the ones that allow user to add or remove from either end of an array.

Add an item to the end of an array: push()

The push() method allows an element to be appended to the end of an existing array.

var someArray = ["itemOne", "itemTwo", "itemThree"]; 

someArray.push("itemFour");

someArray;  // someArray now contains["itemOne", "itemTwo", "itemThree", "itemFour"];

Remove an item from the end of an array: pop()

The pop() method removes the last element from an array. It also returns that removed element.

var lastItem = someArray.pop();

lastItem; // contains "itemFour";

Add an item to the beginning of an array: unshift()

The unshift() method adds an item to the front of an array

someArray.unshift("itemZero");
someArray ; // now contains ["itemZero", "itemOne", "itemTwo", "itemThree"]

Remove an item from the beginning of an array: shift()

The shift() method removes and returns an item from the front of an array

var firstItem = someArray.shift();

firstItem; // contains "itemZero"

JavaScript, a brief history

Hindsight, they say, is 20/20. But being the emotive beings we are, origin stories tend to have an aura of nostalgic romance, even if it be vicariously.

The tale of JavaScript is no exception. From its creation,  the many a mired versions of how it came to be called what it is today, and to the period of hiatus leading to the eventual renaissance, is more than enough to lay ground for a mythical lore.

An Idea is born

The first prototype of JavaScript is said to have been drafted by Brendan Eich. Eich was given just ten days, by Netscape to create a new scripting language for its browser.

Mounting contention with preexisting scripting languages made the notion of a new language tenuous at best, especially when it didn’t even exist. Varying sources list that JavaScript was either based on and/or vying for supremacy amongst other languages – namely “AWK, Java, Perl, Scheme, HyperTalk, and Self” [1][2]. Furthermore, this discussion also eschewed the merit of another language when Java, a fully featured language, already existed.

I dub thee JavaScript

JavaScript was initially code named Mocha by Netscape and then later LiveScript. The now ubiquitous moniker purportedly came about while Netscape was in talks with Sun Microsystems about including Java in their Navigator web browser.

Sources differ about how or why the name came about, but the only unequivocal aspect is that culmination of all deciding factors and entities resulted in LiveScript being renamed JavaScript in late 1995. Whether this was based on riding the popularity of Java or a press release misinterpreted by the media and early target audience will most likely be always up for debate.

Why no love?

The bane of why JavaScript has had its detractors from almost the very first instance to the present is part reason why a disparate language was justified by the pro crowd within Netscape. JavaScript had to have been simple in terms of learning curve and utility i.e. it was meant to be a lightweight and forgiving language that could be used by novices or web designers to assemble the components provided by the more powerful Java counterpart or automate interactions [2] [3]. Java already played the role of the heavy hitting, yet siloed, powerhouse; JavaScript was meant to be based on Java but never compete with it and therefore was a stripped down affair, albeit being a “rich and powerful language” minus the object oriented paradigm [4]. It would be simple enough for novices, yet powerful enough for those skilled enough to use it.

Even though JavaScript was a powerful language, being targeted at novice users, and a slew of other teething issues relegated the language to being ‘simple’ and for the uninitiated by professional programmers [3]. This loathing exists even to the day for mostly the same reasons .

In spite of the tribulations and somewhat windy tenure, JavaScript has endured and is most definitely a key language not to be ignored.

 

[1]Jones, D. (2014) JavaScript: Novice to Ninja. Victoria: SitePoint.

[2] Rauschmayer, A. (2014) Speaking JavaScript – An In-Depth Guide for Programmers. Sebastopol: O’Reilly.

[3] Champeon, S. (2001) JavaScript: How Did We Get Here? O’Reilly Web DevCenter. Available at http://archive.oreilly.com/pub/a/javascript/2001/04/06/js_history.html [Accessed 26 Mar 2016]

[4] Severance, C. (2012) JavaScript: Designing a Language in 10 Days. IEEE Computer Society, 45 (2) pp. 7-8. Available at http://www.computer.org/csdl/mags/co/2012/02/mco2012020007-abs.html

Scavenging old posts

So to populate an otherwise empty blog, I’ve decided to take the quick (cheap) way and just list some of the JavaScript posts I’ve written up in the past. Hopefully worthwhile information and possibly even leads to new posts in the future: