(function() {
"use strict";
//blabla
})();
function hello() {
"use strict";
job = "Hello"; //error
}
function duplicate() {
"use strict";
var obj = {
name : 'yum',
name : 'yum'
}; //error
}
function duplicate(arg, arg) { //error
"use strict";
console.log(arg, arg);
}
function hello(arg) {
"use strict";
arguments[0] = "Hello World"; //no side effect
console.log(arg); //Javascript
}
hello("Javascript");
var max = 3,
count = 0;
function hello() {
"use strict";
while(count < max) {
count++;
console.log("count", count);
arguments.callee(); //error
}
}
hello();
<script data-min="js/app.js" src="js/require.js"></script>
require.config({
path : {
"jquery" : "lib/jquery.js",
"angular" : "lib/angular.js"
},
shim : {
"angular" : {
deps : [jquery],
exports : 'angular'
}
}
});
require(['angular', 'jquery', 'main'], function(angular, $, main) {
main.init();
});
//conf.js
define(function() {
return {
name : "hello"
};
});
//main.js
require(["conf", function(conf) {
return {
init : function() {
console.log(conf.name); //hello
};
};
}]);
//myFactory.js
var forEach = require('mout/array/forEach'),
mixIn = require('mout/object/mixIn'),
map = require('mout/array/map');
var myFactory = function() {
//blabla
};
module.exports = mixIn(myFactory, {
extend : mixIn
});
//app.js
var factory = require('./myFactory');
var result = factory.extend({}, { name : 'hello' });
var myObj = {
name : 'mac',
getName : function() {
return this.name;
},
setName : function(name) {
this.name = name;
}
};
function Car(name, price) {
this.name = name;
this.price = price;
}
Car.prototype.getPrice = function() {
var interest = 10;
return this.price + interest;
};
var car1 = new Car('spring', 100);
console.log(car1.getPrice()); //110
//oh my
var car2 = Car('summer', 100);
console.log(car2.getPrice());
var car = {
name : 'car',
myPrice : 0,
get price() {
var interest = 100;
return this.myPrice + interest;
},
set price(price) {
this.myPrice = price;
}
};
var myCar = Object.create(car);
myCar.name = 'my Car';
myCar.price = 1000;
console.log(myCar.price);
var myCar = Object.create(car, {
name : {
value : 'my car',
enumerable : true,
configurable : true,
writable : true
}
});
Object.defineProperty(myCar, 'name', {
value : 'he'
});
//better
var myCar = Object(car, utilFunc({ name : 'my car' }));
var animal = {
name : 'animal',
meow : function() {
console.log('hello');
}
};
var man = {
name : 'james',
play : function() {
console.log('play');
}
};
var myObj = _.extend({}, man, animal, { name : 'My name' });
myObj.meow(); //hello
myObj.play(); //play
var carFactory = (function() {
var privatePrice = 1000;
return function(parent, prop) {
var newCar = Object.create(arg);
newCar = _.extend(newCar, prop);
newCar.getPrivatePrice = function() { return privatePrice; };
return newCar;
};
})();
var car = { getName : function() {
return this.name + " Hello";
}};
var myCar = carFactory(car, { name : "my car" });
myCar.getPrivatePirce(); //1000
//prototype
var availability = stampit().methods({
getName : function() {
return this.name + "!";
}
});
//own state
var person = stampit().state({
name : "james"
});
//closure
var animal = stampit().enclose(function() {
var price;
return { getPrice : function() { return price; } };
});
var availability = stampit().enclose(function() {
var isOpen = false;
return stampit.mixIn(this, { open : function() {} });
});
var membership = stampit({
add : function() {}
}, {
members : {}
});
var defaults = stampit().state({
name : 'House'
});
var mybar = stampit.compose(defaults, availability, membership);
mybar.add({ name : 'home' }).open();
var curried = _.curry(function(a, b, c) {
console.log(a + b + c);
});
curried(1)(2)(3); //6
curried(1)(2,3); //6
curried(1,2,3); //6
var newFunc = _.partial(function(a, b, c) {
console.log(a + b + c);
}, 1);
newFunc(2, 3); //6
var newFunc = _.bind(function(greeting) {
console.log(greeting + " " + this.name);
}, { name : 'james' }, 'Hello');
newFunc(); //Hello james
var fibonacci = _.memoize(function(n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});
fibonacci(9); //34
var format = function(name) {
return name.upperCase();
};
var greet = function(formatted) {
return "Hello " + formatted + "!";
};
var func = _.compose(greet, format);
func("james'); // Hello JAMES!
var greet = function(name) {
return "Hello " + name + "!";
};
var newFunc = _.wrap(greet, function(func, text) {
console.log('before');
func(text);
console.log('after');
});
newFunc('james');
var throttled = _.throttle(updatePosition, 100);
$(window).on('scroll', throttled);
var searchThrottled = _.throttle(checkAutoComplete, 100, {
'trailing' : true
});
$('#searchQ').on('keydown', searchThrottled);
someAsync(function(error, someData) {
anotherAsync(someData, function(error, anotherData) {
theOtherAsync(anotherData, function(error, result) {
console.log(result);
});
});
});
var promise = new Promise(function(resolve, reject) {
if( /* some condition */) {
resolve('success');
} else {
reject('reject');
}
});
promise.then(function(result) {
console.log(result); //success
}, function(reason) {
console.log(reason); //reject
});