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

Using RSpec to check if something is an instance of another object

The preferred syntax is: expect(@object).to be_a Shirt The older syntax is: @object.should be_an_instance_of Shirt Note that there is a very subtle difference between the two. If Shirt were to inherit from Garment then both of these expectations will pass: expect(@object).to be_a Shirt expect(@object).to be_a Garment If you do and @object is a Shirt, then the … Read more

How to use Ruby’s self keyword

There are several important uses, most of which are basically to disambiguate between instance methods, class methods, and variables. First, this is the best way to define class methods: class Foo def self.bar “class method bar” end def bar “instance method bar” end end Foo.bar #returns “class method bar” foo = Foo.new foo.bar #returns “instance … Read more

How can I access a private constructor of a class?

One way to bypass the restriction is to use reflections: import java.lang.reflect.Constructor; public class Example { public static void main(final String[] args) throws Exception { Constructor<Foo> constructor = Foo.class.getDeclaredConstructor(); constructor.setAccessible(true); Foo foo = constructor.newInstance(); System.out.println(foo); } } class Foo { private Foo() { // private! } @Override public String toString() { return “I’m a Foo … Read more

Non-Singleton Services in AngularJS

I’m not entirely sure what use case you are trying to satisfy. But it is possible to have a factory return instances of an object. You should be able to modify this to suit your needs. var ExampleApplication = angular.module(‘ExampleApplication’, []); ExampleApplication.factory(‘InstancedService’, function(){ function Instance(name, type){ this.name = name; this.type = type; } return { … Read more