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…
Tag: oop
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…
Benefits of prototypal inheritance over classical?
So I finally stopped dragging my feet all these years and decided to learn JavaScript “properly”. One of the most head-scratching elements of the languages design is its implementation of inheritance. Having experience in Ruby, I was really happy to see closures and dynamic typing; but for the life of me can’…
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…
Is there a benefit to defining a class inside another class in Python?
What I’m talking about here are nested classes. Essentially, I have two classes that I’m modeling. A DownloadManager class and a DownloadThread class. The obvious OOP concept here is composition. However, composition doesn’t necessarily mean nesting, right? I have code that looks something l…