new javascript
TRANSCRIPT
new JavaScript;
Самый популярный
№1 на GitHub
время, затраченное на поиск официальной версии логотипа
зага
дочн
ый
субъ
екти
вны
й па
рам
етр
Самый доступный
В каждом браузере
...и не только
Самый простой
{}
f()
В JavaScript нет
● паттерн-матчинга● лист компрехеншнов● континьюэйшнов● корутин● монад● языковой поддержки модулей● огромной стандартной библиотеки● классов с названиями типа
SQLIntegrityConstraintViolationException● и вообще классов
f() {}
f() <: {}
Function — это объектObject — это функция
{}
two = 2;yes = true;obj = {};str = "hello";arr = [];nop = function () {};rex = /dog/;now = new Date;
two = 2;yes = true;obj = {};str = "hello";arr = [];nop = function () {};rex = /dog/;now = new Date;
new
> typeof Date"function"> Date()"Wed Oct 17 2012 18:13:52 GMT+0700 (NOVT)"> new Date()Wed Oct 17 2012 18:13:54 GMT+0700 (NOVT)
> typeof Date()"string"> typeof (new Date())"object"
object.constructor
two.constructor == Number;yes.constructor == Boolean;obj.constructor == Object;str.constructor == String;arr.constructor == Array;nop.constructor == Function;rex.constructor == RegExp;now.constructor == Date;
function Person(first, last) { this.firstName = first; this.lastName = last;}
var bob = new Person('Bob');bob.firstName; // 'Bob'
> bob.constructor === Persontrue> bob instanceof Persontrue
> empty = {}▸ Object> empty.constructorfunction Object() { ... }> empty.toString()"[object Object]"
> empty = {}▾ Object ▾ __proto__: Object ... ▸ constructor: function... ▸ isPrototypeOf: function... ▸ toString: function... ▸ valueOf: function... ...
null
> zen = Object.create(null)▾ Object No Properties> zen instanceof Objectfalse> typeof zen"object"
person = Object.create(null);person.fullName = function () { return this.firstName + ' ' + this.lastName;};
man = Object.create(person);man.sex = 'male';
bob = Object.create(man);bob.firstName = 'bob';bob.lastName = 'smith';
bob.sex; // 'male'bob.fullName(); // 'bob smith'
> bob▾ Object firstName: "bob" lastName: "smith" ▾ __proto__: Object
sex: "male" ▾ __proto__: Object
▸ fullName: function...
null
sex "male"
fullName function () { ... }
firstName "bob"
lastName "smith"
person
man
bob
function Person(first, last) { this.firstName = first; this.lastName = last;}
var bob = new Person('Bob');bob.firstName; // 'Bob'
function Person(first, last) { this.firstName = first; this.lastName = last; this.fullName = function () { return this.firstName + ' ' + this.lastName; }}
firstName "bob"
lastName "smith"
fullName function () ...
bob
firstName "alice"
lastName "jones"
fullName function () ...
alice
Constructor.prototype
function Person(first, last) { this.firstName = first; this.lastName = last;}
Person.prototype.fullName =function () { return this.firstName + ' ' + this.lastName;}
firstName "bob"
lastName "smith"
bob
firstName "alice"
lastName "jones"
alice
fullName function () ...
Person.prototype
f()
Объекты первого класса
function foo() { return 'FOO';}
var bar = foo;bar(); // 'FOO'
function baz(f) { return f();}
baz(foo); // 'FOO'
function quux() { return foo;}
quux()(); // 'FOO'
var code = 'return "FOO"'
var foo = new Function(code);foo(); // 'FOO'
Область видимости переменных
var
function fred(x) { var a = 10; return a * x;}
var a = 3;fred(5); // 50console.log(a); // 3
function fred(x) { /*no var*/ a = 10; return a * x;}
var a = 3;fred(5); // 50console.log(a); // 10
this
Person.prototype.fullName =function () { return this.firstName + ' ' + this.lastName;}
bob = new Person('bob');bob.fullName();// 'bob undefined'
xavier = { firstName: 'xavier', lastName: 'sanchez'};
xavier.fullName(); // error!
xavier.fullName = Person.prototype.fullName;
xavier.fullName(); // 'xavier sanchez'
Person.prototype .fullName .call(xavier); // 'xavier sanchez'
that
function Logger(s) { s.on('data', function (d) { this.log(d); });}
Logger.prototype.log =function (data) { console.log(new Date, data);}
function Logger(s) { s.on('data', function (d) { this.log(d); });}
function Logger(s) { var that = this; s.on('data', function (d) { that.log(d); });}
function Logger(s) { s.on('data', Logger.prototype .log.bind(this));}
@stevebesthttps://github.com/stevebest
jifasnif