combinando oo e funcional em javascript de forma prática
TRANSCRIPT
Antes de comear...
> typeof NaN === "number"true>
> "1" == 1true> "1" === 1false>
> typeof []'object'> Object.prototype.toString.call([])'[object Array]'
Tipos dinmicos
Tipos fracos
Tipos dinmicos
Tipos fracos
Tipos dinmicos
Orientado a prottipos
Tipos fracos
Tipos dinmicos
Orientado a prottipos
No tem classes
Tipos fracos
Tipos dinmicos
Orientado a prottipos
No tem classes
No tem interfaces
Tipos fracos
Tipos dinmicos
Orientado a prottipos
No tem classes
No tem interfaces
No tem packages
Tipos fracos
Tipos dinmicos
Orientado a prottipos
No tem classes
No tem interfaces
No tem packages
No tem contexto Private
Tipos fracos
Tipos dinmicos
Orientado a prottipos
No tem classes
No tem interfaces
No tem packages
No tem contexto Private
No tem contexto Protected
No tem mtodos
ECMA 262, 3rd Edition, December 1999), pgina 2 (4.2):
"An ECMAScript object is an unordered collection of properties each with zero or more attributes..."
ECMA 262, 3rd Edition, December 1999), pgina 2 (4.2):
"... and a method is a function associated with an object via a property."
ECMA 262, 5.1 Edition, June 2011), pgina 2 (4.2):
" A function that is associated with an object via a property is a method."
ECMA 262, 5.1 Edition, June 2011), pgina 2 (4.2):" A function that is associated with an object via a property is a method."
ECMA 262, 3rd Edition, December 1999), pgina 2 (4.2):"... and a method is a function associated with an object via a property."
Objeto Literal
> var objeto = {... propriedade: "Propriedade"... }> objeto{ propriedade: 'Propriedade' }>
Objeto Literal
> var objeto = {... propriedade: "Propriedade"... }> objeto{ propriedade: 'Propriedade' }>
JSON?
Tudo objeto?
> 1.0.toString();'1'>
Tudo objeto?
> 1.toString();...
Tudo objeto?
Feita em 10 dias
https://twitter.com/#!/diveintomark/status/112607722704343040
Como se divertir com uma linguagem dessas?
Combinando Programao funcional e Orientao a Objetos em Javascript de forma prtica
@cmilfont
QCon So Paulo 2011
Minimalismo e Simplicidade
> var empresa = {... denominacaoSocial: "Milfont Evil Corp"... }> empresa.cnpj === "9999999999999" }false>
> var empresa = {... denominacaoSocial: "Milfont Evil Corp"... }> empresa.cnpj === "9999999999999" }false> empresa.endereco.sede === "Capitolio"TypeError: Cannot read property 'sede' of undefined
> if(empresa.endereco && empresa.endereco.sede && empresa.endereco.sede.numero === "Capitolio")
ruby-1.8.7-p330 :012 > empresa.try(:endereco).try(:sede) => nilruby-1.8.7-p330 :012 > empresa.try(:endereco).try(:sede) == "teste" => false
> Object.prototype.try = function(){};> empresa.try("empresa.endereco.sede.numero");
> Object.prototype.try = function(){};> empresa.try("empresa.endereco.sede.numero");
Object.defineProperty(Object.prototype, "try", { value: function(){} });
Object.defineProperty(Object.prototype, "try", { value: function(){} });
Problemas de Extender built-in
https://github.com/kriskowal/es5-shim
Object.defineProperty(Object.prototype, "try", { value: function(){} });
Object.defineProperty(Object.prototype, "try", { value: function(){} });
Object.defineProperty(Object.prototype, "trying", { value: function(){} });
var markup = " ";
markup = " ";
var markup = " ";
Object.defineProperty(String.prototype, 'interpolate', { enumerable: false, value: function(values, pattern) { var pattrn = pattern || /\#\{([^}]+)\}/g; return this.replace(pattrn, function(match, value){ var result = values[value]; return (result)? result: ""; }); }});
https://github.com/documentcloud/underscore
DRY
var empresa = { nome: $("input#nome"), endereco: $("input#endereco")});
jQuery.ajax({ data : empresa, cache : false, dataType : 'json', error : error, contentType : "application/json", headers : {"Content-Type":"application/json", "Accept":"application/json"}, success : callback, type : "POST", url : "/empresas"});
var empresa = { nome: $("input#nome"), endereco: $("input#endereco")});
jQuery.ajax({ data : empresa, cache : false, dataType : 'json', error : error, contentType : "application/json", headers : {"Content-Type":"application/json", "Accept":"application/json"}, success : callback, type : "POST", url : "/empresas"});
class EmpresasController < ApplicationController
end
jQuery.ajax({ data : empresa, type : "POST", url : "/empresas"});
class EmpresasController < ApplicationController
end
rake routesPOST /empresas(.:format) {:action=>"create", :controller=>"empresas"}
jQuery.ajax({ data : empresa, type : "POST", url : "/empresas"});
class EmpresasController < ApplicationController
end
rake routesPOST /empresas(.:format) {:action=>"create", :controller=>"empresas"}
jQuery.ajax({ data : empresa, type : "POST", url : "/empresas"});
class EmpresasController < ApplicationController
end
rake routesPOST /empresas(.:format) {:action=>"create", :controller=>"empresas"}
var empresas = new EmpresasControllerempresas.create(empresa, callback)
jQuery.ajax({ data : empresa, type : "POST", url : "/empresas"});
var empresa = { nome: $("input#nome"), endereco: $("input#endereco")});
jQuery.ajax({ data : empresa, cache : false, dataType : 'json', error : error, contentType : "application/json", headers : {"Content-Type":"application/json", "Accept":"application/json"}, success : callback, type : "POST", url : "/empresas"});
var empresa = { nome: $("input#nome"), endereco: $("input#endereco")});
var empresas = new EmpresasControllerempresas.create(empresa, callback)
var empresas = new EmpresasControllerempresas.create(empresa, callback);
function EmpresasController() { this.create = function(){};}
var empresas = new EmpresasControllerempresas.create(empresa, callback);
var Interface = { this.routes = { action: { url: "/controller", method: "POST" }};
var empresas = new EmpresasControllerempresas.create(empresa, callback);
function EmpresasController() { this.create = function(){};}
var Interface = { this.routes = { action: { url: "/controller", method: "POST" }};
var empresas = new EmpresasControllerempresas.create(empresa, callback);
function EmpresasController() {}EmpresasController.prototype = DJR;
var Interface = { this.routes = { action: { url: "/controller", method: "POST" }};
function EmpresasController() {}EmpresasController.prototype = DJR;
function EmpresasController() { DJR.call(this);}
function EmpresasController() { this.useDJR = DJR; this.userDJR();}
function EmpresasController() { DJR.call(this);}
var InterfaceDJR = function(){ this.routes = { create: { url: "/controller", method: "POST" } }};
EmpresasController.prototype = InterfaceDJR;
function EmpresasController() { DJR.call(this);}var DJR = function() { this.ajax = function(object, callback, error, method, url, params) { jQuery.ajax({ context : self, data : object, cache : false, dataType : 'json', error : error, success : callback, type : method, url : url }); };};
var DJR = function() { this.ajax = function(action, args) { var json = args[0]; var callback = args[1]; $.ajax({ url: this.route[action].url, type: this.route[action].method, data: JSON.stringify(json) }); } this.__noSuchMethod__ = function(){ this.ajax(arguments[0], arguments[1]); }};
var DJR = function() {};
DJR.algumMetodo = function(){};DJR['algumMetodo'] = function(){};
var DJR = function() { this.ajax = function(action, args) { } for (var action in this.route) { this[action] = function() { this.ajax(act,arguments); } };
};
var DJR = function() { this.ajax = function(action, args) { } for (var action in this.route) { this[action] = function(act){ return function() { this.ajax(act,arguments); } }(action); };
};
var empresa = { nome: $("input#nome"), endereco: $("input#endereco")});
var empresas = new EmpresasControllerempresas.create(empresa, callback)
var empresa = $("form#empresas").getJSON();
var empresas = new EmpresasControllerempresas.create(empresa, callback)
sammy.post('#empresas', function() { var empresa = $("form#empresas").getJSON(); new EmpresasController() .create(empresa, callback)});
var valorDosDebitos = lancamento.partidas .filter(function(partida){ return partida.natureza === Partida.DEBITO; }).map(function(partida){ return partida.valor; }).reduce(function(previousValue, currentValue, index, array){ return previousValue + currentValue; }, 0);