Steve Yegge recently posted an interesting blog post on what he calls the universal design pattern. In there he details using prototypes as a modelling tool, instead of classes. I like the way this introduces less coupling compared to inheritance. But that is something one can get with classes as well, by implementing classes in terms of...
In another question a member asserted "I would advice you not to use prototypes. They have their uses, but not for most cases and definitely not in this one."
Can anyone elaborate on why this might be true (or otherwise)? I almost always supply prototypes for my Perl functions, and I've never before seen anyone else say anything bad ab...
Could you explain the difference between setting methods in the constructor and through prototype object? The following code shows these two ways of setting the methods - say_hello and say_bye both work fine:
function MessageClass() {
this.say_bye = function() { alert('see ya'); };
}
MessageClass.prototype.say_hello = function() { al...
In Javascript, every object has a prototype. Any prototype is an object, therefore any prototype has a prototype.
This is me:
var Person = function (name) {
this.name = name;
}
Person.prototype = {
walk: function () {
alert(this.name + ' is walking');
}
}
var walter = new Person('Walter');
I can walk:
walter.walk() // => "Wa...
I understand from this post, that it's an anti-pattern to modify Object's prototype in JavaScript. I was curious, however, if it's widely considered to be an antipattern to modify other 'built-in' prototypes.
For example: say we wanted to add a setPixel(x,y,color) function to CanvasRenderingContext2D - to abstract out the duty of get...
What is a prototype for a javascript class? In other words, what is the different between
Example.prototype.method {}
and
Example.method{}
when defining the Example class?
edit: for those interested, i found a great explanation (in addition to the answer below) here for the difference between class methods and constructor methods...
Hi all,
This is about "inheritance" in JavaScript.
Supose I create a constructor Bird(), and another called Parrot() which I make to "inherit" the props of Bird by asigning an instance of it to Parrot's prototype, like the following code shows:
function Bird() {
this.fly = function(){};
}
function Parrot() {
this.talk = funct...
I have been using Perl for some time, but today I came across this code:
sub function1($$)
{
//snip
}
What does this mean in Perl?
...
All frameworks aside, what are some of the common helper functions/prototype methods you use on a daily basis?
Please note I am not arguing against frameworks. I've simply found that the majority of what I do on a daily basis can, most often, be done with a few dozen Array, String and Element.prototype methods. With the addition of a fe...
i'm confused about how to do inline functions in C++....
lets say this function. how would it be turned to an inline function
int maximum( int x, int y, int z )
{
int max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max;
}
...
I have only recently started programming significantly, and being completely self-taught, I unfortunately don't have the benefits of a detailed Computer science course. I've been reading a lot about JavaScript lately, and I'm trying to find the benefit in classes over the prototype nature of JavaScript. The question seems to be drawn dow...
updated 2010-06-15T09:45:00Z:
added an example for the "classes as instances" approach, and an explanation of the "instances as classes" approach; incorporated and referenced Alex Martelli's answer;
I'm wondering how to implement prototypal inheritance in Python. There would seem to be two different approaches to this problem: class...
Im thinking of making a custom datatypes / prototypes for a project im working on but im wondering if its such a good idea?
For example
class String
{
var $Value;
private $escaped = false;
function __construct($String)
{
$this->Value = $String;
}
function escape()
{
if($escaped === false)
...
Possible Duplicate:
JavaScript: Class.method vs. Class.prototype.method
What's the difference between creating a prototype like this:
Date.foo = function(bar) {
alert(bar);
};
And this:
Date.prototype.foo = function(bar) {
alert(bar);
};
Why/when should I use either?
...
At work, we use jQuery. Shortly after we started using it, I saw that a couple developers were adding functions to a file jquery-extensions.js. Inside, I found a whole bunch of methods added to $ that essentially amount to static methods on jQuery. Here's a few:
$.formatString(str, args) {
...
}
$.objectToArray(obj) {
...
}
E...
I've been looking into Javascript prototyping recently, can understand the theory, and believe it can be very useful to my understanding of the language, but can't quite get the following to work...
var player = function(){//Declarations here};
var super_player.prototype = new player();
Every compiler/checker flags a 'missing semicolo...
I'd like to fake a namespace in Javascript. This can be done as follows:
var cars = {};
cars.car = function() {
...
}
cars.car.prototype = {
drive: function() {
...
}
}
Works fine. My question, however, is if I can directly fill the whole namespace with JSON, like this:
var cars = {
car: function() {
....
In the linked SO answer, Eric illustrates a way to call a subroutine, which accepts arrays by reference as arguments, and use the prototypes to allow the caller code to pass the array names without using reference operator \@; the way built-ins like push @array, $value do.
# Original code:
sub Hello { my ($x_ref, $y_ref) = @_; ...}
Hell...
I maintain an open source program that builds with autoconf.
Right now I'm having a problem with some of my users. They are using a pre-distributed VM from an organization that has an incorrect prototype for strchr in it. Their prototype is:
char *strchr(char *,int c);
when of course we know it should be:
char *strchr(const char *s...