Successfully reported this slideshow.

JavaScript patterns

2,240 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

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();}

×