Your SlideShare is downloading. ×
  • Like
JavaScript patterns
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

JavaScript patterns

  • 1,786 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,786
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
6
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. functon some () { “use strict”; // do something... ubar = ‘dedededfe’ // error}
  • 2. for (var i = 0; i < myarray.length; i++ ) { // = some}for (var i = 0, max = myarray.length; i < max ;i++ ) { // = some}
  • 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. var zero = 0;if (zero === false) { // good}if (zero == false) { // anti pattern}
  • 5. // anti patternparseInt(“012”) => 10// good wayparseInt(“012”, 10) => 12Number(“012”) => 12
  • 6. var Person function(name) { this.name = name; this.say = function(){ return “ test “ + this.name };}var person = new Person(“aaa”);
  • 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. 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. var Waffle = function() { var that = { msg : ‘yummy’ }; return that;}var waffle = new Waffle(); // => {msg: ‘yummy’};
  • 10. function Waffle() { if(!(this instanceof Waffle)) { return new Waffle(); } this.tastes = ‘yummy’ ;}var waffle = Waffle(); // is instance of Waffle
  • 11. try { throw { name : “someError”, message : “error test”, test : function() { return name } };} catch (e) { console.log(e.test());}
  • 12. try { throw { name : “someError”, message : “error test”, errorStatus : 312 };} catch (e) { return [message, e.errorStatus];}
  • 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. 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. 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. $(‘div1’).onclick = function () { alert (‘aaa’);}$(‘div2’).onclick = function () { alert (‘bbb’);}....
  • 17. $(‘div1’).onclick = gen_alert(‘aaa’);$(‘div2’).onclick = gen_alert(‘bbb’);function gen_alert(msg) { return function { alert(msg) };}
  • 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();}