jsCoreTalk



jsCoreTalk

0 0


jsCoreTalk

A short talk about the core JavaScript APIs that define "the sensible subset of JavaScript"

On Github clux / jsCoreTalk

"Everyone should be upset with JavaScript"

--Eirik http://github.com/clux

JS

DOM + AJAX APIs

Canvas, WebGL, Websockets, Webworkers, Web components

NodeJS

CoffeeScript, TypeScript, ASM.js

Core language ->

Core language

operators, syntax, keywords, inheritance

timers

scope and closures

original 8 built in types

Prototypes

function User(name) {
  this.name = name;
}
User.locate = function (name) { .. };
User.prototype.greet = function () { return 'hi: ' + this.name; }


var me = new User('Eirik');
me instanceof User; // true








// where does properties on `me` live?
// NB: me.property === me['property']


// me.qwfp
// User.prototype.qwfp
// Object.prototype.qwfp
// -> undefined

Object

{key: value, ...}

.keys

.create

.freeze

.isFrozen

.seal

::toString

::valueOf

JSON global

Array

[a, b, c]

::toString

::join

::concat

::sort

::reverse ::slice

::forEach ::map ::filter ::reduce

::indexOf ::lastIndexOf

::pop ::push ::shift ::unshift

::every ::some

String

"ab©"

::toString

::slice

::indexOf

::replace

::match

::toUpperCase

::toLowerCase

::trim

Date

new Date()

.now

.parse

::valueOf

::toJSON

::otherHelpfulMethods

Boolean

true,false

::toString

::valueOf

Number

123

::toString

.isFinite

.isNaN

Math global

RegExp

/^match[u|i]$/

::toString

::test

Function

function (args) { body; }

::toString

::call

::apply

::bind

Call/Apply/Bind

var fn = function () { code(); }
// all equivalent
fn(arg1, arg2, arg3);
fn.call(this, arg1, arg2, arg3);
fn.apply(this, [arg1, arg2, arg3]);
fn.bind(this, arg1, arg2, arg3)();
fn.bind(this, arg1, arg2)(arg3);
fn.bind(this, arg1)(arg2, arg3);
fn.bind(this)(arg1, arg2, arg3);

this

function User(name) {
	this.name = name;
}

for (var i = 0; i < 5; i += 1) ;
console.log(i) // 5




















User.prototype.context = function () {
	return this;
};
var ea = new User('eirik');
ea.context(); // { name: 'eirik' }
this; // global object

User.prototype.context = function () {
	return (function () {
		return this;
	})();
};
ea.context(); // global object




User.prototype.context = function () {
	return (function () {
		return this;
	}).call(this);
};
ea.context(); // { name: 'eirik' }



User.prototype.context = function () {
	var that = this;
	return (function () {
	  return that;
	})();
};

poe.js

Once upon a midnight dreary, while I struggled with jQuery, Sighing softly, weak and weary, troubled by my daunting chore, While I grappled with weak mapping, suddenly a function wrapping formed a closure, gently trapping objects that had gone before. Ah, distinctly I remember, it was while debugging Ember, As each separate dying member left its host for ever more. Eagerly I wished the morrow–vainly I had sought to borrow (From my bookmarked trail of sorrow), APIs from Underscore. There I sat engaged in guessing the meaning of each cursed expression, Endless callbacks in procession; nameless functions, nothing more, This and more I sat divining, strength and spirit fast declining, Disclose the value we’re assigning! Tell me - tell me, I implore!