Share
Embed
JS: Leave the classes to those other languages!
Giamir Buoncristiani
April 22, 2016
Transcript
58 slides
-
IDEA! Surely someone has been in my same position and
maybe he/she implemented some workaround. -
OBJECTS IN JS come in a literal or a constructed
form are collections of key/value pairs have a prototype -
THE LITERAL FORM car __proto__ color: ‘red’ Object.prototype // literal
form var car = { color: ‘red’ }; built-in -
THE CONSTRUCTED FORM car __proto__ color: ‘red’ Object() .prototype //
constructed form var car = new Object(); car.color = ‘red’; Object.prototype .constructor built-in -
WHAT IS A FUNCTION? is a sub-type of object ("callable
object") function Vehicle() { this.color = ‘red’; } can be handled like any other plain object Vehicle() .prototype Vehicle.prototype automatically get a property called prototype, which is just an empty object. -
WHAT IS A CONSTRUCTOR? is any function which is used
as a constructor function Vehicle() { this.color = ‘red’; } var myVehicle = new Vehicle(); I can use Vehicle as a constructor -
A “NEW” CONFUSION What happens when a constructor is called?
var myVehicle = new Vehicle(); 1. It creates a new object 2. It sets the constructor property of the object to Vehicle 3. It sets up the object to delegate to Vehicle.prototype 4. It calls Vehicle() in the context of the new object myVehicle __proto__ Vehicle() .prototype Vehicle.prototype .constructor color: ‘red’ -
ES5 OBJECT.CREATE() myVehicle __proto__ color: ‘red’ Vehicle.prototype var myVehicle =
Object.create(Vehicle.prototype); myVehicle.color = ‘red’; -
PROTOTYPE CHAIN __proto__ shape triangle __proto__ border: ‘red’ null __proto__
Object.prototype built-in var shape = { border: ‘red’ }; var triangle = Object.create( shape ); triangle.border; triangle.area; // ‘red’ // undefined -
CLASSIC INHERITANCE class Person attr_reader :name def initialize(name) @name =
name end end class Student < Person attr_reader :school def initialize(name, school) super(name) @school = school end end giamir = Student.new('Giamir', 'TWU') puts giamir.name # Giamir puts giamir.school # TWU Classes are blue-prints Objects are copies of all the characteristics described by classes. -
PROTOTYPAL INHERITANCE function Person(name) { this.name = name; } Person.prototype.getName
= function() { return this.name; }; function Student(name,school) { Person.call( this, name ); // pseudo-polymorphism this.school = school; } Student.prototype = Object.create(Person.prototype); Student.prototype.getSchool = function() { return this.school; }; var giamir = new Student('Giamir','TWU'); giamir.getName(); // "Giamir" giamir.getSchool(); // "TWU" -
PSEUDOCLASSES STYLE function Person(name) { this.name = name; } Person.prototype.getName
= function() { return this.name; }; function Student(name,school) { Person.call( this, name ); // pseudo-polymorphism this.school = school; } Student.prototype = Object.create(Person.prototype); Student.prototype.getSchool = function() { return this.school; }; var giamir = new Student('Giamir','TWU'); giamir.getName(); // "Giamir" giamir.getSchool(); // "TWU" -
PSEUDOCLASSES STYLE function Person(name) { this.name = name; } Person.prototype.getName
= function() { return this.name; }; function Student(name,school) { Person.call( this, name ); // pseudo-polymorphism this.school = school; } Student.prototype = Object.create(Person.prototype); Student.prototype.getSchool = function() { return this.school; }; var giamir = new Student('Giamir','TWU'); giamir.getName(); // "Giamir" giamir.getSchool(); // "TWU" -
PSEUDOCLASSES STYLE function Person(name) { this.name = name; } Person.prototype.getName
= function() { return this.name; }; function Student(name,school) { Person.call( this, name ); // pseudo-polymorphism this.school = school; } Student.prototype = Object.create(Person.prototype); Student.prototype.getSchool = function() { return this.school; }; var giamir = new Student('Giamir','TWU'); giamir.getName(); // "Giamir" giamir.getSchool(); // "TWU" -
PSEUDOCLASSES STYLE Person.prototype Student.prototype getName Object.prototype built-in __proto__ Object() .prototype
Person() .prototype Student() .prototype __proto__ -
PSEUDOCLASSES STYLE function Person(name) { this.name = name; } Person.prototype.getName
= function() { return this.name; }; function Student(name,school) { Person.call( this, name ); // pseudo-polymorphism this.school = school; } Student.prototype = Object.create(Person.prototype); Student.prototype.getSchool = function() { return this.school; }; var giamir = new Student('Giamir','TWU'); giamir.getName(); // "Giamir" giamir.getSchool(); // "TWU" -
PSEUDOCLASSES STYLE Person.prototype Student.prototype getName __proto__ Object.prototype built-in __proto__ Object()
.prototype Person() .prototype Student() .prototype -
PSEUDOCLASSES STYLE function Person(name) { this.name = name; } Person.prototype.getName
= function() { return this.name; }; function Student(name,school) { Person.call( this, name ); // pseudo-polymorphism this.school = school; } Student.prototype = Object.create(Person.prototype); Student.prototype.getSchool = function() { return this.school; }; var giamir = new Student('Giamir','TWU'); giamir.getName(); // "Giamir" giamir.getSchool(); // "TWU" -
PSEUDOCLASSES STYLE Person.prototype Student.prototype getSchool getName __proto__ Object.prototype built-in __proto__
Object() .prototype Person() .prototype Student() .prototype -
PSEUDOCLASSES STYLE function Person(name) { this.name = name; } Person.prototype.getName
= function() { return this.name; }; function Student(name,school) { Person.call( this, name ); // pseudo-polymorphism this.school = school; } Student.prototype = Object.create(Person.prototype); Student.prototype.getSchool = function() { return this.school; }; var giamir = new Student('Giamir','TWU'); giamir.getName(); // "Giamir" giamir.getSchool(); // "TWU" -
PSEUDOCLASSES STYLE Person.prototype Student.prototype giamir getSchool getName __proto__ name school
__proto__ Object.prototype built-in __proto__ Object() .prototype Person() .prototype Student() .prototype INHERITANCE -
PSEUDOCLASSES STYLE Person.prototype Student.prototype giamir getSchool getName __proto__ name school
__proto__ Object.prototype built-in __proto__ Object() .prototype Person() .prototype Student() .prototype INHERITANCE DELEGATION -
WHAT IS OLOO? is a style pattern to implement delegation
oriented design stands for objects linked to other objects is a much simpler and straightforward way to work in JS gets rid of the confusing “new” operator -
OLOO STYLE var Person = { init: function(name) { this.name
= name; }, getName: function() { return this.name; } }; var Student = Object.create(Person); Student.set = function(name, school) { this.init(name); this.school = school; }; Student.getSchool = function() { return this.school; }; var giamir = Object.create(Student); giamir.set('Giamir', 'TWU'); giamir.getName(); // Giamir giamir.getSchool(); // TWU -
OLOO STYLE var Person = { init: function(name) { this.name
= name; }, getName: function() { return this.name; } }; var Student = Object.create(Person); Student.set = function(name, school) { this.init(name); this.school = school; }; Student.getSchool = function() { return this.school; }; var giamir = Object.create(Student); giamir.set('Giamir', 'TWU'); giamir.getName(); // Giamir giamir.getSchool(); // TWU -
OLOO STYLE var Person = { init: function(name) { this.name
= name; }, getName: function() { return this.name; } }; var Student = Object.create(Person); Student.set = function(name, school) { this.init(name); this.school = school; }; Student.getSchool = function() { return this.school; }; var giamir = Object.create(Student); giamir.set('Giamir', 'TWU'); giamir.getName(); // Giamir giamir.getSchool(); // TWU -
OLOO STYLE var Person = { init: function(name) { this.name
= name; }, getName: function() { return this.name; } }; var Student = Object.create(Person); Student.set = function(name, school) { this.init(name); this.school = school; }; Student.getSchool = function() { return this.school; }; var giamir = Object.create(Student); giamir.set('Giamir', 'TWU'); giamir.getName(); // Giamir giamir.getSchool(); // TWU -
OLOO STYLE Person Student set getSchool init getName __proto__ __proto__
Object.prototype built-in -
OLOO STYLE var Person = { init: function(name) { this.name
= name; }, getName: function() { return this.name; } }; var Student = Object.create(Person); Student.set = function(name, school) { this.init(name); this.school = school; }; Student.getSchool = function() { return this.school; }; var giamir = Object.create(Student); giamir.set('Giamir', 'TWU'); giamir.getName(); // Giamir giamir.getSchool(); // TWU -
OLOO STYLE Person Student giamir set getSchool init getName __proto__
__proto__ __proto__ Object.prototype built-in -
OLOO STYLE var Person = { init: function(name) { this.name
= name; }, getName: function() { return this.name; } }; var Student = Object.create(Person); Student.set = function(name, school) { this.init(name); this.school = school; }; Student.getSchool = function() { return this.school; }; var giamir = Object.create(Student); giamir.set('Giamir', 'TWU'); giamir.getName(); // Giamir giamir.getSchool(); // TWU -
OLOO STYLE Person Student giamir set getSchool init getName __proto__
__proto__ __proto__ Object.prototype built-in name school OBJECTS LINKED TO OTHER OBJECTS OLOO -
WRAPPING UP can be avoided adopting delegation oriented design faking
classes introduces another level of complexity GETTING OUT OF OUR COMFORT ZONE IS THE KEY TO LEARN classes are an optional design pattern for code