Upgrade to Pro — share decks privately, control downloads, hide ads and more …

new JavaScript

new JavaScript

Введение в язык JavaScript для чайников

Avatar for Stepan Stolyarov

Stepan Stolyarov

October 19, 2012

Other Decks in Programming

Transcript

  1. {}

  2. f()

  3. В JavaScript нет • паттерн-матчинга • лист компрехеншнов • континьюэйшнов

    • корутин • монад • языковой поддержки модулей • огромной стандартной библиотеки • классов с названиями типа SQLIntegrityConstraintViolationException • и вообще классов
  4. {}

  5. two = 2; yes = true; obj = {}; str

    = "hello"; arr = []; nop = function () {}; rex = /dog/; now = new Date;
  6. two = 2; yes = true; obj = {}; str

    = "hello"; arr = []; nop = function () {}; rex = /dog/; now = new Date;
  7. new

  8. > 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)
  9. two.constructor == Number; yes.constructor == Boolean; obj.constructor == Object; str.constructor

    == String; arr.constructor == Array; nop.constructor == Function; rex.constructor == RegExp; now.constructor == Date;
  10. function Person(first, last) { this.firstName = first; this.lastName = last;

    } var bob = new Person('Bob'); bob.firstName; // 'Bob'
  11. > empty = {} ▸ Object > empty.constructor function Object()

    { ... } > empty.toString() "[object Object]"
  12. > empty = {} ▾ Object ▾ __proto__: Object ...

    ▸ constructor: function... ▸ isPrototypeOf: function... ▸ toString: function... ▸ valueOf: function... ...
  13. > zen = Object.create(null) ▾ Object No Properties > zen

    instanceof Object false > typeof zen "object"
  14. person = Object.create(null); person.fullName = function () { return this.firstName

    + ' ' + this.lastName; }; man = Object.create(person); man.sex = 'male';
  15. > bob ▾ Object firstName: "bob" lastName: "smith" ▾ __proto__:

    Object sex: "male" ▾ __proto__: Object ▸ fullName: function...
  16. null sex "male" fullName function () { ... } firstName

    "bob" lastName "smith" person man bob
  17. function Person(first, last) { this.firstName = first; this.lastName = last;

    } var bob = new Person('Bob'); bob.firstName; // 'Bob'
  18. function Person(first, last) { this.firstName = first; this.lastName = last;

    this.fullName = function () { return this.firstName + ' ' + this.lastName; } }
  19. firstName "bob" lastName "smith" fullName function () ... bob firstName

    "alice" lastName "jones" fullName function () ... alice
  20. function Person(first, last) { this.firstName = first; this.lastName = last;

    } Person.prototype.fullName = function () { return this.firstName + ' ' + this.lastName; }
  21. f()

  22. var

  23. function fred(x) { var a = 10; return a *

    x; } var a = 3; fred(5); // 50 console.log(a); // 3
  24. function fred(x) { /*no var*/ a = 10; return a

    * x; } var a = 3; fred(5); // 50 console.log(a); // 10
  25. Person.prototype.fullName = function () { return this.firstName + ' '

    + this.lastName; } bob = new Person('bob'); bob.fullName(); // 'bob undefined'
  26. function Logger(s) { s.on('data', function (d) { this.log(d); }); }

    Logger.prototype.log = function (data) { console.log(new Date, data); }