11. functon some () {
“use strict”;
// do something...
ubar = ‘dedededfe’ // error
}
12.
13.
14. for (var i = 0; i < myarray.length; i++ ) {
// = some
}
for (var i = 0, max = myarray.length; i < max ;i++ ) {
// = some
}
15.
16.
17. 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
18.
19. var zero = 0;
if (zero === false) {
// good
}
if (zero == false) {
// anti pattern
}
24. var Person function(name) {
this.name = name;
this.say = function(){ return “ test “ + this.name };
}
var person = new Person(“aaa”);
25.
26. 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”;
27.
28. 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 or
global symbol ‘name’
29. var Waffle = function() {
var that = { msg : ‘yummy’ };
return that;
}
var waffle = new Waffle(); // => {msg: ‘yummy’};
30. function Waffle() {
if(!(this instanceof Waffle)) {
return new Waffle();
}
this.tastes = ‘yummy’ ;
}
var waffle = Waffle(); // is instance of Waffle
31. try {
throw {
name : “someError”,
message : “error test”,
test : function() { return name }
};
} catch (e) {
console.log(e.test());
}
33. function Gadget () {
this.name = “aaaa”;
this.getName = function () { return name};
this.setName = function (n) { name = n };
}
var g = new Gadget();
g.name = “changed”;
34. 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 see
this”
35. 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 see
this”