codecamp

Sea.js 快速入门

快速指南

还记得jQuery如何使用的么?Sea.js也是如此。例子在这里可以找到,用anywhere起个静态服务来看看。

首先写个模块:

// File:js/module/greet.js
define(function (require, exports) {
    function helloPython() {
        document.write("Hello,Python");
    }
    function helloJavaScript() {
        document.write("Hello,JavaScript");
    }
    exports.helloPython = helloPython;
    exports.helloJavaScript = helloJavaScript;
});

如果你对Node.js非常熟悉,你可以把这个模块理解为Node.js的模块加上一个Wrapper。

在页面中引入Sea.js:

<!-- File:index.html -->
<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Getting start with Sea.js</title>
    <!-- 引入seajs-->
    <script src="//atts.w3cschool.cn/attachments/image/wk/helloseajs/sea.js"></script>
</head>
<body>

</body>
</html>

加载模块文件!

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Getting start width Sea.js</title>
    <!-- 引入seajs-->
    <script src="//atts.w3cschool.cn/attachments/image/wk/helloseajs/sea.js"></script>
    <script>
        seajs.use(['/js/module/greet'], function (Greet) {
            Greet.helloJavaScript()
        })
    </script>
</head>
<body>

</body>
</html>

看到页面上输出的Hello,JavaScript么,这确实太简单了!


Sea.js 是什么?
Sea.js 使用指南
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }