요즘 한창 재미난 Node.js 관련 링크들 중 개인적으로 책갈피한 내용들
밑의 문서는 원서 내용 발췌. (한글번역 문서 : http://nodeguide.atelier.weaveus.com/index.html)
Felix's Node.js Guide 링크중에서
Using EventEmitters
Node.js implements the observer pattern using a class called EventEmitter. Whenever there is an object that represents the source of several kinds of events, node.js usually makes the underlaying class inherit from EventEmitter.
Using EventEmitter's is pretty straight-forward. You can listen to a specific event by calling the 'on()' function on your object, providing the name of the event, as well as a callback closure as the parameters. For example:
var data = '';
req
.on('data', function(chunk) {
data += chunk;
})
.on('end', function() {
console.log('POST data: %s', data);
})
As you can see, the on()
function also returns a reference to the object it belongs to, allowing you to chain several of such event listeners.
If you're only interested in the first occurrence of an event, you can use the once()
function instead.
Finally, you can remove event listeners by using the removeListener
function. Please note that the argument to this function is a reference to the callback you are trying to remove, not the name of the event:
var onData = function(chunk) {
console.log(chunk);
req.removeListener(onData);
}
req.on('data', onData);
The example above is essentially identical to the once()
function.
Felix's Node.js Style Guide 링크 중에서
Tabs vs Spaces
Let's start with the religious problems first. Our benevolent dictator has chosen 2 space indention for the node core, so you would do well to follow his choice.
Line length
Limit your lines to 80 characters. Yes, screens have gotten much bigger over the last few years, but your brain hasn't. Use the additional room for split screen, your editor supports that, right?
Class names
Class names should be capitalized using upper camel case.
Right:
function BankAccount() {
}
Wrong:
function bank_Account() {
}
Conditions
Any non-trivial conditions should be assigned to a descriptive variable:
Right:
var isAuthorized = (user.isAdmin() || user.isModerator());
if (isAuthorized) {
console.log('winning');
}
Wrong:
if (user.isAdmin() || user.isModerator()) {
console.log('losing');
}
Named closures
Feel free to give your closures a name. It shows that you care about them, and will produce better stack traces:
Right:
req.on('end', function onEnd() {
console.log('winning');
});
Wrong:
req.on('end', function() {
console.log('losing');
});
'Frontend > Javascript' 카테고리의 다른 글
[Vue.js] Vue.js 개발 입문하면서 궁금했던 기초 질문들 & 소소한 링크들 (0) | 2019.07.22 |
---|---|
[Node.js] 관련 링크들 (3) | 2012.06.03 |
[dojo] Dijit과 친해지기 2탄 (2) | 2008.11.20 |
[dojo] Dijit과 친해지기 1탄 (0) | 2008.11.20 |
innerHTML (4) | 2007.03.03 |