How does Python’s “super” do the right thing?

Change your code to this and I think it’ll explain things (presumably super is looking at where, say, B is in the __mro__?): class A(object): def __init__(self): print “A init” print self.__class__.__mro__ class B(A): def __init__(self): print “B init” print self.__class__.__mro__ super(B, self).__init__() class C(A): def __init__(self): print “C init” print self.__class__.__mro__ super(C, self).__init__() class …

Read more

Creating a Fragment: constructor vs newInstance()

I personally find that using constructors is a much more common practice than knowing to use newInstance() and passing parameters. The factory method pattern is used fairly frequently in modern software development. So basically my question is, why does Google not want you to use constructors with parameters for Fragments? You answered your own question: …

Read more

JavaScript: The Good Parts – How to not use `new` at all

Crockford gives an example for an object creation function as should have been provided by JS itself in one of his Javascript talks available on http://developer.yahoo.com/yui/theater/ However, the YUI(3) team itself uses “new”, and they DO follow his recommendations (since he’s the Yahoo chief JS architect (UPDATE: he moved on, but the statement was true …

Read more

Javascript: Mocking Constructor using Sinon

I needed a solution for this because my code was calling the new operator. I wanted to mock the object that the new call created. var MockExample = sinon.stub(); MockExample.prototype.test = sinon.stub().returns(“42”); var example = new MockExample(); console.log(“example: ” + example.test()); // outputs 42 Then I used rewire to inject it into the code that …

Read more

Setter methods or constructors

You should use the constructor approach, when you want to create a new instance of the object, with the values already populated(a ready to use object with value populated). This way you need not explicitly call the setter methods for each field in the object to populate them. You set the value using a setter …

Read more

new Backbone.Model() vs Backbone.Model.extend()

There is a basic difference, which in short can be described as “the difference between the project of a house and the house itself”. For expert programmers I would just say that “new Backbone.Model” returns an object instance, but “Backbone.Model.extend” returns a constructor function FIRST: a new object (i.e. The house) var TestModel = new …

Read more