Namespace
/Unit
에 내 코드를 래핑하는 가장 우아한 방법을 제공하는 메서드를 사용하려고합니다. 예를 들어 Google지도 API의 var a = Google.Maps.Foo();
은 매우 깨끗하게 보입니다. (즉, 올바른 용어 인 경우)JavaScript에서 네임 스페이스/클래스 형식 구조를 만드는 가장 우아한 방법
은 나뿐만 아니라 묶으에 jQuery
No Conflict
$
기호를 싶습니다.
지금까지 내가 좋아하는 오전 :
// Top level container for sub objects
var myApp = myApp || {};
// An object to be held in myApp
(function(skillet, $, undefined) {
//Private Property
var isHot = true;
//Public Property
skillet.ingredient = "Bacon Strips";
//Public Method
skillet.fry = function() {
var oliveOil;
addItem("\t\n Butter \n\t");
addItem(oliveOil);
console.log("Frying " + skillet.ingredient);
return "Fried!";
};
//Private Method
function addItem(item) {
if (item !== undefined) {
console.log("Adding " + $.trim(item));
}
}
}(window.myApp.skillet = window.myApp.skillet || {}, jQuery));
사람이 잠재적 인 문제를 지적하고,이를 확장하거나 일반적으로 더 나은 방법을 제공 할 수 있습니까?
우아함이기 때문에 무언가를 할 수있는 * 가장 * 우아한 방법은 결코 없다 주걱. – Philipp
당신을 위해 최선의 결정을 내리는 데 필요한 모든 것은 여기에 있습니다 : http://stackoverflow.com/questions/881515/javascript-namespace-declaration – NinjaNye
@NinjaNye 아, 네, 그 코드는 어디에서 확장하려고합니까? 정말. –