Skip to content

Instantly share code, notes, and snippets.

@rudolph9
Last active August 29, 2015 14:06
Show Gist options
  • Save rudolph9/908a23019ad471ef86d3 to your computer and use it in GitHub Desktop.
Save rudolph9/908a23019ad471ef86d3 to your computer and use it in GitHub Desktop.
A good example of javascript prototypal functions.
> Node = function(){};
[Function]
> Node.pro
Node.propertyIsEnumerable
Node.prototype
> Node.prototype.foo = {bar: 'hello world'};
{ bar: 'hello world' }
> node = new Node();
{}
> node.foo
{ bar: 'hello world' }
> node1 = new Node();
{}
> node1.foo
{ bar: 'hello world' }
> node.foo.bar
'hello world'
> node.foo.bar = 'something else';
'something else'
> node1.foo
{ bar: 'something else' }
> node2 = new Node();
{}
> node2.foo
{ bar: 'something else' }
>
// Somthing similar but with creating the objects
// on the new objects rather than prototypes.
> Node = function(){
... this.foo = { bar: 'hello world' }
... };
[Function]
> Node
[Function]
> node = new Node()
{ foo: { bar: 'hello world' } }
> node.foo
{ bar: 'hello world' }
> node1 = new Node()
{ foo: { bar: 'hello world' } }
> node1.foo
{ bar: 'hello world' }
> node.foo.bar = 'something else'
'something else'
> node1.foo
{ bar: 'hello world' }
> node2 = new Node();
{ foo: { bar: 'hello world' } }
> node2.foo.bar
'hello world'
> node.foo.bar
'something else'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment