functon some () {  “use strict”;  // do something...  ubar = ‘dedededfe’ // error}
for (var i = 0; i < myarray.length; i++ ) {  // = some}for (var i = 0, max = myarray.length; i < max ;i++ ) {  // = some}
var man = { heads : 2, legs : 2 };Object.prototype.clne = function() {};for (var i in man) {  if (man.hasOwnProperty(i)) {...
var zero = 0;if (zero === false) {   // good}if (zero == false) {   // anti pattern}
// anti patternparseInt(“012”) => 10// good wayparseInt(“012”, 10) => 12Number(“012”)       => 12
var Person function(name) {  this.name = name;  this.say = function(){ return “ test “ + this.name };}var person = new Per...
function Gadget () {  var name = “aaaa”;    this.getName = function () { return name};    this.setName = function (n) { na...
var Person = function(name) {  this.name = name;  this.say = function(){ return “ test “ + this.name };}var person = Perso...
var Waffle = function() {  var that = { msg : ‘yummy’ };  return that;}var waffle = new Waffle(); // => {msg: ‘yummy’};
function Waffle() {  if(!(this instanceof Waffle)) {    return new Waffle();  } this.tastes = ‘yummy’ ;}var waffle = Waffle(); ...
try {  throw {    name : “someError”,    message : “error test”,    test : function() { return name }  };} catch (e) {  co...
try {   throw {      name : “someError”,      message : “error test”,      errorStatus : 312   };} catch (e) {   return [m...
function Gadget () {  this.name = “aaaa”;    this.getName = function () { return name};    this.setName = function (n) { n...
var blog = Blog.load( {blog_id: 2} );var entry = blog.entries().first();var logger = function(msg) {   console.log(“Blog : ...
var blog = Blog.load( {blog_id: 2} );var entry = blog.entry();function cusutom_logger(blog_id, entry_id) {  return functio...
$(‘div1’).onclick = function () {   alert (‘aaa’);}$(‘div2’).onclick = function () {   alert (‘bbb’);}....
$(‘div1’).onclick = gen_alert(‘aaa’);$(‘div2’).onclick = gen_alert(‘bbb’);function gen_alert(msg) {  return function { ale...
typedef int (*CALLBACK)(* void); // pointer type for int func(void *)typedef struct {    CALLBACK handler;    void * optio...
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
JavaScript patterns
Upcoming SlideShare
Loading in...5
×

JavaScript patterns

1,874

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,874
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JavaScript patterns

  1. 1. functon some () { “use strict”; // do something... ubar = ‘dedededfe’ // error}
  2. 2. for (var i = 0; i < myarray.length; i++ ) { // = some}for (var i = 0, max = myarray.length; i < max ;i++ ) { // = some}
  3. 3. var man = { heads : 2, legs : 2 };Object.prototype.clne = function() {};for (var i in man) { if (man.hasOwnProperty(i)) { console.log(i, ‘:’, man[i]); } # => heads:2} # legs:2
  4. 4. var zero = 0;if (zero === false) { // good}if (zero == false) { // anti pattern}
  5. 5. // anti patternparseInt(“012”) => 10// good wayparseInt(“012”, 10) => 12Number(“012”) => 12
  6. 6. var Person function(name) { this.name = name; this.say = function(){ return “ test “ + this.name };}var person = new Person(“aaa”);
  7. 7. function Gadget () { var name = “aaaa”; this.getName = function () { return name}; this.setName = function (n) { name = n };}var g = new Gadget(); g.getName(); g.setName(‘test’);g.name = “change”;
  8. 8. var Person = function(name) { this.name = name; this.say = function(){ return “ test “ + this.name };}var person = Person(“aaa”);person.name // => this will reference window.name orglobal symbol ‘name’
  9. 9. var Waffle = function() { var that = { msg : ‘yummy’ }; return that;}var waffle = new Waffle(); // => {msg: ‘yummy’};
  10. 10. function Waffle() { if(!(this instanceof Waffle)) { return new Waffle(); } this.tastes = ‘yummy’ ;}var waffle = Waffle(); // is instance of Waffle
  11. 11. try { throw { name : “someError”, message : “error test”, test : function() { return name } };} catch (e) { console.log(e.test());}
  12. 12. try { throw { name : “someError”, message : “error test”, errorStatus : 312 };} catch (e) { return [message, e.errorStatus];}
  13. 13. function Gadget () { this.name = “aaaa”; this.getName = function () { return name}; this.setName = function (n) { name = n };}var g = new Gadget();g.name = “changed”;
  14. 14. var blog = Blog.load( {blog_id: 2} );var entry = blog.entries().first();var logger = function(msg) { console.log(“Blog : “ + blog.id + “ Entry :“ + entry.id + msg)};logger(“see this”) / /=> “Blog id : 2 Entry id : 3 seethis”
  15. 15. var blog = Blog.load( {blog_id: 2} );var entry = blog.entry();function cusutom_logger(blog_id, entry_id) { return function(msg) { console.log(“Blog : “ + blog_id + “ Entry :“ + entry_id + msg)};}var logger = cusutom_logger(blog.id, entry.id);logger(“see this”) / /=> “Blog id : 2 Entry id : 3 seethis”
  16. 16. $(‘div1’).onclick = function () { alert (‘aaa’);}$(‘div2’).onclick = function () { alert (‘bbb’);}....
  17. 17. $(‘div1’).onclick = gen_alert(‘aaa’);$(‘div2’).onclick = gen_alert(‘bbb’);function gen_alert(msg) { return function { alert(msg) };}
  18. 18. typedef int (*CALLBACK)(* void); // pointer type for int func(void *)typedef struct { CALLBACK handler; void * option;} callback_data;callback_data callback;void register_callback (CALLBACK f, void* opt) { callback.handler = f; callback.option = opt;}void invoke_callback() { CALLBACK f = callback.handler; f(callback.option);}char * msg = “message”;int speak() { printf (“%s”, (char *)void) };main () { register_callback((CALLBACK)speak, (void *)msg); invoke_callback();}
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×