developer

Use of alloc init instead of new

Question: ❓❓❓ Learning Objective-C and reading sample code, I notice that objects are usually created using this method: SomeObject *myObject = [[SomeObject alloc] init]; instead of: SomeObject *myObject = [SomeObject new]; Is there a reason for this, as I have read that they are equivalent? Good Answer: 👌👌👌 There are Read more…

By admin, ago
developer

What does ‘super’ do in Python?

Question: ❓❓❓ What’s the difference between: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() and: class Child(SomeBaseClass): def __init__(self): SomeBaseClass.__init__(self) I’ve seen super being used quite a lot in classes with only single inheritance. I can see why you’d use it in multiple inheritance but am unclear as to what the advantages Read more…

By admin, ago
developer

JavaScript private methods

Question: ❓❓❓ To make a JavaScript class with a public method I’d do something like: function Restaurant() {} Restaurant.prototype.buy_food = function(){ // something here } Restaurant.prototype.use_restroom = function(){ // something here } That way users of my class can: var restaurant = new Restaurant(); restaurant.buy_food(); restaurant.use_restroom(); How do I create Read more…

By admin, ago
developer

Constructors in JavaScript objects

Question: ❓❓❓ Can JavaScript classes/objects have constructors? How are they created? Good Answer: 👌👌👌 Using prototypes: function Box(color) // Constructor { this.color = color; } Box.prototype.getColor = function() { return this.color; }; Hiding “color” (somewhat resembles a private member variable): function Box(col) { var color = col; this.getColor = function() Read more…

By admin, ago