admin管理员组文章数量:1221352
I try to understand the examples on angularjs. For example the following code snippet. How does this .value
and .factory
and .config
work?
angular.module('project', ['ngRoute', 'firebase'])
.value('fbURL', '/')
.factory('Projects', function($firebase, fbURL) {
return $firebase(new Firebase(fbURL)).$asArray();
})
.config(function($routeProvider) {
...
EDIT:
My point of not understanding the code above is the blank line after the first line and the line starting with a dot.
I try to understand the examples on angularjs.org. For example the following code snippet. How does this .value
and .factory
and .config
work?
angular.module('project', ['ngRoute', 'firebase'])
.value('fbURL', 'https://angularjs-projects.firebaseio.com/')
.factory('Projects', function($firebase, fbURL) {
return $firebase(new Firebase(fbURL)).$asArray();
})
.config(function($routeProvider) {
...
EDIT:
My point of not understanding the code above is the blank line after the first line and the line starting with a dot.
Share Improve this question edited Dec 21, 2014 at 20:53 hol asked Dec 21, 2014 at 20:29 holhol 8,4235 gold badges35 silver badges61 bronze badges 6 | Show 1 more comment5 Answers
Reset to default 13As standard dictates, unless there's a reason for ASI (like using postfix op, return
, continue
, throw
or break
statements), line terminators that separate language tokens are treated by JS as any other whitespace - that is, ignored. So these lines...
angular.module(arg1, arg2)
.value(arg3)
.controller(arg4)
.filter(arg5)
... are treated essentially the same as ...
angular.module(arg1, arg2).value(arg3).controller(arg4).filter(arg5)
So it's all about readability.
A function in javascript can return an object that you can call a function on as well. Basically you can chain function calls. This is the same thing as, for instance, jQuery does. Let's say you have an object that has the following methods:
setFirstValue()
setSecondValue()
If you return the same object from within those methods (eg.) :
function setFirstValue() { do something here; return this; }
You can now chain these methods together:
object.setFirstValue().setSecondValue()
So .. as you can see .. the dot actually operates on the result of the previous operation - it's as simple as that.
This is the same as:
var app = angular.module('project', ['ngRoute', 'firebase'])
app.value('fbURL', 'https://angularjs-projects.firebaseio.com/')
app.factory('Projects', function($firebase, fbURL) {
return $firebase(new Firebase(fbURL)).$asArray();
})
app.config(function($routeProvider) {
...
The angular.module call returns an object, and the dot notation references the previous object returned.
Hopefully that helps you understand how the referencing of objects works in the way you've set it up.
To answer your updated question:
Javascript ignores the line breaks and the spacing. It simply reads it as .module().value().factory.config(), but it is formatted in a human readable manner.
.
operates on the result of the preceding expression. For example, angular.module('project', ['ngRoute', 'firebase'])
returns a module object, which you can call .value
on.
本文标签: angularjsHow JavaScript treats lines starting with a dotStack Overflow
版权声明:本文标题:angularjs - How JavaScript treats lines starting with a dot? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739339574a2158870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.value(
immediately after'firebase'])
. – RemcoGerlich Commented Dec 21, 2014 at 20:38angular
. This is just a form of convenience: a developer does not need to repeat the object a method is called on. – try-catch-finally Commented Dec 21, 2014 at 20:42