Last active
August 29, 2015 14:06
-
-
Save rudolph9/908a23019ad471ef86d3 to your computer and use it in GitHub Desktop.
A good example of javascript prototypal functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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