当前位置: 首页 » 产品 » 微商货源 » 正文

Require.JS中的几种define定义方式教程

放大字体  缩小字体 发布日期: 2024-09-25 16:42   来源:http://www.baidu.com/  作者:无忧资讯  浏览次数:12
核心提示:定义简单的键值对define({color: black,size: unisize }); 定义不带依赖的函数define(function () {//Do setup work herereturn

定义简单的键值对

define({ color: "black", size: "unisize" });

定义不带依赖的函数

define(function () { //Do setup work here return { color: "black", size: "unisize" } });

存在依赖的函数式定义

如果模块存在依赖:则第一个参数是依赖的名称数组;第二个参数是函数,在模块的所有依赖加载完毕后,该函数会被调用来定义该模块,因此该模块应该返回一个定义了本模块的object。依赖关系会以参数的形式注入到该函数上,参数列表与依赖名称列表一一对应。

define(["http://www.jsgho.com/help/fwq/cart", "http://www.jsgho.com/help/fwq/inventory"], function(cart, inventory) { //return an object to define the "my/shirt" module. return { color: "blue", size: "large", addToCart: function() { inventory.decrement(this); cart.add(this); } } } );

将模块定义为一个函数

对模块的返回值类型并没有强制为一定是个object,任何函数的返回值都是允许的。此处是一个返回了函数的模块定义:

define(["my/cart", "my/inventory"], function(cart, inventory) { //return a function to define "foo/title". //It gets or sets the window title. return function(title) { return title ? (window.title=title) : inventory.storeName + ' ' + cart.name; } } );

定义一个命名模块

你可能会看到一些define()中包含了一个模块名称作为首个参数:

define("foo/title", ["my/cart", "my/inventory"], function(cart, inventory) { //Define foo/title object in here. } );

 
 
[ 产品搜索 ]  [ 加入收藏 ]  [ 告诉好友 ]  [ 打印本文 ]  [ 违规举报 ]  [ 关闭窗口 ]

 

 
推荐图文
推荐产品
点击排行
    行业协会  备案信息  可信网站