以下示例将展示创建命名空间的高级方式 JS将拥有私有和共有属性和方法并且可以具有类似JAVA继承的功能Here is their example showing how to declare private & public properties and functions. Everything is done as a self-executing anonymous function.
(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 );
};
//Private Method
function addItem( item ) {
if ( item !== undefined ) {
console.log( "Adding " + $.trim(item) );
}
}
}( window.skillet = window.skillet || {}, jQuery ));
//Adding new Functionality to the skillet (function( skillet, $, undefined ) { //Private Property var amountOfGrease = "1 Cup"; //Public Method skillet.toString = function() { console.log( skillet.quantity + " " + skillet.ingredient + " & " + amountOfGrease + " of Grease" ); console.log( isHot ? "Hot" : "Cold" ); }; }( window.skillet = window.skillet || {}, jQuery ));
MY DEMO:
/** * 公告推送JS * Created by 许畅 on 2015-05-29. */ (function(notice, $, undefined) { /** * 公告推送初始化 * * @return {[type]} [description] */ notice.init = function() { var intro = introJs(); //注册退出事件 intro.onexit(function() { notice.exitNotice(); }); //注册步骤完成事件 intro.oncomplete(function() { notice.stepComplete(); }); //注册步骤改变事件 intro.onchange(function(targetElement) { notice.stepChange(targetElement); }); intro.setOptions({ steps: scope.steps, nextLabel: "下一条", prevLabel: "上一条", skipLabel: "跳过", doneLabel: "关闭" }); intro.start(); for (var i = 0; i < scope.steps.length; i++) { if (i > 0) { $(scope.steps[i].element).hide(); } }; //CAP工作台容器 $(".workbench-container").hide(); }; /** * 退出公告 * * @return {[type]} [description] */ notice.exitNotice = function() { var closedNoticeIds = []; scope.allNoticeIds.forEach(function(item, index, array) { if ($.inArray(item, scope.readNoticeIds) == -1 && item != scope.currentNoticeId) { closedNoticeIds.push(item); } }); dwr.TOPEngine.setAsync(false); CapNoticeAction.closeNotice(closedNoticeIds, scope.currentNoticeId, { callback: function(result) {}, errorHandler: function(message, exception) {} }); dwr.TOPEngine.setAsync(true); //刷新公告iframe $('#mainIframe').attr('src', $('#mainIframe').attr('src')); $("#noticeIntroduce").hide(); $(".workbench-container").show(); }; /** * 公告步骤完成 * * @return {[type]} [description] */ notice.stepComplete = function() {}; /** * 步骤改变事件 * * @param {[type]} targetElement [description] * @return {[type]} [description] */ notice.stepChange = function(targetElement) { dwr.TOPEngine.setAsync(false); CapNoticeAction.readNotice({ id: scope.currentNoticeId }, { callback: function(result) { //不存在readNoticeIds数组中则添加 if ($.inArray(scope.currentNoticeId, scope.readNoticeIds) == -1) { scope.readNoticeIds.push(scope.currentNoticeId); } //下个公告为当前公告id scope.currentNoticeId = targetElement.id; }, errorHandler: function(message, exception) { scope.currentNoticeId = targetElement.id; } }); dwr.TOPEngine.setAsync(true); //前端下一步显示处理 $("#" + targetElement.id).show(); scope.steps.forEach(function(item, index, array) { if (item.element != ("#" + targetElement.id)) { $(item.element).hide(); } }); }; }(window.notice = window.notice || {}, jQuery)); var scope = null; angular.module('noticeIntroduce', ["ngSanitize"]).controller('noticeIntroduceController', function($scope, $timeout) { $scope.introduce = {}; $scope.readNoticeIds = []; //已阅的公告id $scope.currentNoticeId = ""; //当前公告id $scope.allNoticeIds = []; $scope.ready = function() { $scope.init(); scope = $scope; }; /** * 页面初始化 * * @return {[type]} [description] */ $scope.init = function() { dwr.TOPEngine.setAsync(false); CapNoticeAction.queryCurrentUserCanPushsNotice({ callback: function(result) { var steps = []; if (result && result.length > 0) { result.forEach(function(item, index, array) { //处理前端id不能为数字问题 item.id = "_" + item.id; steps.push({ element: "#" + item.id, intro: item.title }); if (index == 0) { $scope.currentNoticeId = item.id; } $scope.allNoticeIds.push(item.id); }); } else { $("#noticeIntroduce").hide(); } $scope.steps = steps; $scope.introduce.notices = result; }, errorHandler: function(message, exception) { $scope.introduce.notices = []; } }); dwr.TOPEngine.setAsync(true); }; }); //jquery初始化公告推送信息 $(function() { if (scope.steps && scope.steps.length > 0) { notice.init(); } });