Created
April 8, 2015 13:46
-
-
Save queerviolet/e82dd47e2278a0747f07 to your computer and use it in GitHub Desktop.
JSLN specimen #003 livecode
This file contains 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
function p() { | |
// call console.log with this=console and all our arguments | |
// as positional args. | |
console.log.apply(console, arguments); | |
} | |
p('hello'); | |
p('hello', 'world'); | |
function hello(x, y) { | |
console.log(this.message, x, y); | |
} | |
hello.apply({message: 'DIE DIE DIE'}); | |
var x = {message: "I'm an object!", func: hello}; | |
x.func('arg1', 'arg2'); | |
hello.apply({message: 'LIVE LIVE LIVE'}, ['arg1', 'arg2']); | |
hello.call({message: 'CALL CALL CALL'}, 'carg1', 'carg2'); | |
function toAry() { | |
return Array.prototype.slice.apply(arguments); | |
} | |
p(toAry(1, 2, 3), 'and', 'it', 'doesnt', 'matter', 'how', 'many', 'args'); | |
function Animal() { | |
this.kingdom = 'squishy'; | |
} | |
function Cat() { | |
// Animal.apply(this); | |
Animal.call(this); | |
this.type = 'cat'; | |
} | |
Cat.prototype = Object.create(Animal.prototype); | |
Cat.prototype.constructor = Cat; | |
p(new Cat().kingdom); | |
This file contains 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
// This goat's name property is held in the closure. | |
function createGoat(name) { | |
return { | |
setName: function(newName) { | |
name = newName; | |
}, | |
getName: function() { | |
return name; | |
}, | |
}; | |
} | |
var goat = createGoat('frankie'); | |
console.log('goat.getName():', goat.getName()); | |
goat.setName('alice'); | |
console.log('after set goat.getName():', goat.getName()); | |
console.log('goat.name:', goat.name); | |
var Ungulate = (function() { | |
var nextGoatId = 0; | |
var goatsTeleported = 0; | |
function goatConstructor(name) { | |
this.name = name; | |
this.id = ++nextGoatId; | |
} | |
goatConstructor.prototype.teleport = function() { | |
console.log('teleporting', this.name); | |
++goatsTeleported; | |
}; | |
goatConstructor.getNumTeleported = function() { | |
return goatsTeleported; | |
} | |
return goatConstructor; | |
})(); | |
var bessie = new Ungulate('bessie'); | |
var sue = new Ungulate('sue'); | |
console.log('bessie.id:', bessie.id, 'sue.id:', sue.id); | |
bessie.teleport(); | |
sue.teleport(); | |
console.log('ungulates teleported:', Ungulate.getNumTeleported()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment