Moderators: phlip, Moderators General, Prelates
Well of course it is important if a language feature introduces additional room for programmers to make mistakes. Still, it depends on what you want; you may find the benefits larger than the tradeoffs. Lots of professional programmers are happily using C even though pointers are rather tricky business.Zamfir wrote:If I understand it correctly, the main complaints about new are
A. if you forget to use it, the code screws up silently. I lack the experience to judge if this is important, or a minor issue.
I doubt whether this was the real motive for introducing the keyword 'new'. It's a solution to a problem. It's not the only possible solution, and perhaps not the best, but apparently it seemed like a good idea at the time. Once you choose this strategy, I definitely think 'new' is best possible name for the construct. Also note that 'new' in Java in fact doesn't mean the same as 'new' in C++. Such keywords have different meanings in different languages, simply because it would be impossible for them to have the same meaning.Zamfir wrote:B. new is a hack to make the language look misleadingly Java-esque with classes.
function Vector (x, y) {
this.x = x;
this.y = y;
this.length = function() { return Math.sqrt(this.x * this.x + this.y * this.y); };
}function Vector (x, y) {
var self = { };
self.x = x;
self.y = y;
self.length = function() { return Math.sqrt(this.x * this.x + this.y * this.y); };
// in the line above perhaps replace 'this' by 'self', not sure
return self;
}Yep. IMHO the constructor approach is the most convenient.Zamfir wrote:While on the plus side:
C. You often want a constructor function for objects anyway.
Well... "good" is a subjective notion. But as I said, I think the constructor approach can be very practical.Zamfir wrote:D. Separating templates from instances is good, even if javsascript doesn't enforce that separation
Zamfir wrote:D. Separating templates from instances is good, even if javsascript doesn't enforce that separation
function List(){...}
List.prototype.add = function(){...}
List.prototype.index = function(){...}
List.prototype.map = function(){...}
...
function InfiniteList(){/* loops when you try to access past the end */}
InfiniteList.prototype = new List();
InfiniteList.prototype.index = function(){...}
...
var a = new InfiniteList(1,2,3);
console.log(a.length); // logs "0"
function InfiniteList(){...};
InfiniteList.prototype = Object.create(List.prototype, {});
InfiniteList.prototype.index = function(){...};
function makeList() {
var items = [];
return {
add: function add(item) { items.push(item); },
length: function length() { return items.length; }
}
}
function makeInfiniteList() {
var list = makeList();
return {
add: function add(item) { list.add(item); },
length: function length() { return Infinity; }
}
}
function makeBetterList() {
var list = makeList();
list.isEmpty = function isEmpty() { return this.length() === 0; }
return list;
}
List.prototype.isEmpty = function() {
return this.length == 0;
}
Zamfir wrote:Another question, if you allow me: how much use do you make of inheritance in javascript? So not the single-level inheritance to make instances, but extensions like your InfiniteList above.
I personally find that I use inheritance a fair amount in static languages, since it plays nice with the IDE and compile-time sanity checking. But I used to do a lot of Python programming in the past, and there I rarely derived a class from another. In prety much every case where it might have made sense, I found it clearer to use composition, to include the 'base' class as property of other classes instead of parent. Does that sound familiar?
function Paint() {}; // superclass for all paint objects
function SolidColor(color) {
this.color = color;
}
SolidColor.prototype = new Paint();
...
function LinearColorPatch(tl,tr,br,bl) {
...
}
LinearColorPatch.prototype = new Paint();
Users browsing this forum: No registered users and 6 guests