Skip to content

Tag: oop

Constructor function vs Factory functions

Can someone clarify the difference between a constructor function and a factory function in Javascript. When to use one instead of the other? Answer The basic difference is that a constructor function is used with the new keyword (which causes JavaScript to automatically create a new object, set this within t…

In Javascript, what does this underscore mean?

I’m reading a tutorial on backbone.js here: http://addyosmani.com/blog/building-spas-jquerys-best-friends/ What are the underscores? (_index, _photos, _album) Why use them? Answer It means private fields or private methods. Methods that are only for internal use. They should not be invoked outside of th…

Does JavaScript have classes?

A friend and I had an argument last week. He stated there were no such things as classes in JavaScript. I said there was as you can say var object = new Object() He says “as there is no word class used. It’s not a class.” Who is right? Edit: July 2017 JavaScript classes introduced in ECMAScr…

How to get a JavaScript object’s class?

I created a JavaScript object, but how I can determine the class of that object? I want something similar to Java’s .getClass() method. Answer There’s no exact counterpart to Java’s getClass() in JavaScript. Mostly that’s due to JavaScript being a prototype-based language, as opposed t…

Associative arrays in javascript

I have this object: Its used in this way: The problem is, in the addField() function the fields array is being set correct (perhaps because a numerical index is being used to refer to it) but the other 2 arrays (labels and rules) aren’t being touched at all. Doing a console.log shows them as empty in fi…