(functionints(arrOfNumberStr) { 'use strict'; return arrOfNumberStr.map(toInt); var toInt = function(str) { returnparseInt(str, 10); } })(['11', '11']); -> UncaughtTypeError: undefined is not a function(…)
优先使用function declaration
Why? Function declarations are named, so they’re easier to identify in call stacks. Also, the whole body of a function declaration is hoisted, whereas only the reference of a function expression is hoisted. This rule makes it possible to always use Arrow Functions in place of function expressions. – from javascript style guide
下篇会记录Javascript的作用域,Global Scope, Lexical Scope, Dynamic Scope, Function Scope
JavaScript has a feature known as implied globals. Whenever a name is used, the interpreter walks the scope chain backwards looking for a var statement for that name. If none is found, that variable is assumed to be global. If it’s used in an assignment, the global is created if it doesn’t already exist. This means that using or creating global variables in an anonymous closure is easy. Unfortunately, this leads to hard-to-manage code, as it’s not obvious (to humans) which variables are global in a given file.
$q can be used in two fashions — one which is more similar to Kris Kowal’s Q or jQuery’s Deferred implementations, and the other which resembles ES6 promises to some degree.
ES6 style
The streamlined ES6 style promise is essentially just using $q as a constructor which takes a resolver function as the first argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// assume that $q and $resource are available. function = asyncGo() { return $q( function(resolve, reject) { $resource("www.example.com", {}, {}) .get({}, function(result) { resolve("hello, " + result); }, function(error) { reject("sorry, " + error + " is not allowed"); }) }); };
var promise = asyncGo(); promise.then(function(greeting){ console.log(greeting); }, function(err){ console.log(err); });
Deferred style
The purpose of the deferred object is to expose the associated Promise instance as well as APIs that can be used for signaling the successful or unsuccessful completion, as well as the status of the task.
resolve(value) – resolves the derived promise with the value. If the value is a rejection constructed via $q.reject, the promise will be rejected instead.
reject(reason) – rejects the derived promise with the reason. This is equivalent to resolving it with a rejection constructed via $q.reject.
notify(value) - provides updates on the status of the promise’s execution. This may be called multiple times before the promise is either resolved or rejected.
1 2 3 4 5 6
promiseB = promiseA.then(function(result) { return result + 1; });
// promiseB will be resolved immediately after promiseA is resolved and its value // will be the result of promiseA incremented by 1
$q vs. $rootScope
$q is integrated with the $rootScope.Scope Scope model observation mechanism in angular
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
it('should simulate promise', inject(function($q, $rootScope) { var deferred = $q.defer(); var promise = deferred.promise; var resolvedValue;
// Simulate resolving of promise deferred.resolve(123); // Note that the 'then' function does not get called synchronously. // This is because we want the promise API to always be async, whether or not // it got called synchronously or asynchronously. expect(resolvedValue).toBeUndefined();
// Propagate promise resolution to 'then' functions using $apply(). $rootScope.$apply(); expect(resolvedValue).toEqual(123); }));
then
then that can accept three arguments(three callback types)–success, failure, progress. for defer object, resolve will apply to success, reject apply to failure and notify apply to progress.
a defer object contains always, done, fail methods! so the always and done will trigger if resovle be called and return a promise object, which contains resolved result.
好像问题也不大。然而,实际上开关不会只控制灯泡,还有冰箱,电视,空调呢?我们终于发现,其实开关的职责不是去开关灯泡,而是开合电源。but who cares the electrinic? 所以我们假设电源就是一只信号,某位ruby大牛把所有对象的调用,都看作是向这些对象发送message。用在这里,倒是很合理。
好了,我们有业务细节A了,又有业务A的directive了。我们理所应当地认为这个directive和A应该是一体的,为什么不直接将A细节写入directive的scope中呢?答案是X, Y, Z在这里是可以复用这个directive的,所以不能硬编码。不能硬编码,我首先的直觉就是变量替换,又要求复用,那么就立马想到了继承或者组合。又因为组合优于继承(更现实的是我不知道如何在AngularJS中实现继承),我决定使用组合。接下来这个过程的产出物就是我们的卖品,构造如下: