有人可以解释这个require.js示例中涉及的语法吗? [英] Can someone explain the syntax involved in this require.js example?

查看:112
本文介绍了有人可以解释这个require.js示例中涉及的语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个问题不是太宽泛,但是在这个特定的require.js / ESRI示例中有很多不熟悉的语法,我希望有人可以向我解释一些。

I hope this question isn't too broad, but there's a LOT of unfamiliar syntax happening in this particular require.js/ESRI example, and I am hoping someone can explain some of this to me.

首先,这段代码可以工作(也就是说,它可以完成我期望它做的事情):它创建一个基本地图并添加一个从地图服务中提取的FeatureLayer。它是ESRI Javascript API页面中的一个示例的精简版本。这是代码:

First, this code works (that is, it does what I expect it to do): it creates a base map and adds a FeatureLayer pulled from a map service. It's a stripped-down version of an example from the ESRI Javascript API page. Here's the code:

    var map;
    var featureLayer;

    require(["esri/map", "dojo/domReady!", "esri/layers/FeatureLayer"], function (Map) {
        map = new Map("map", {
            basemap: "topo",
            center: [-100.195, 39.567], // long, lat
            zoom: 4
        });

        featureLayer = new esri.layers.FeatureLayer(
            "http://my-server-url.com/arcgis/rest/services/Projects/MapServer/0",
            {
                mode: esri.layers.FeatureLayer.MODE_ONDEMAND
            }
        );

        map.addLayer(featureLayer);

    });

现在针对特定问题:


  1. 这需要什么([...],函数(args){}语法?我甚至不知道怎么读这个。是不是对require.js的函数调用?方括号中有什么?函数参数是什么?

  1. What is this require([...], function(args) { } syntax doing? I don't even know how to read this. Is it a function call to require.js? What goes in the square brackets? What are the function arguments?

从其他例子来看,它看起来通常应该是在require.js调用中,每个包含一个函数参数。但是在这里,如果我为FeatureLayer添加一个参数,它就不起作用。

From other examples, it looks like there normally should be one function argument per include in the require.js call. But here, if I add an argument for FeatureLayer, it doesn't work.

dojo / domReady!include似乎在任何例子中都没有相应的参数。这是否与感叹号有关?感叹号是什么意思?

The "dojo/domReady!" include doesn't seem to ever have a corresponding argument in any example. Is this related to the exclamation point? What does the exclamation point mean?

有人能指出一个USEFUL require.js参考吗?requirejs.org网站更像是技术规范而不是用户手册。而且ESRI网站似乎假设你知道如何使用require。

Can anyone point me to a USEFUL require.js reference? The requirejs.org website reads more like a tech spec than a user manual. And the ESRI website seems to assume you know how to use require.

是的,我一直都是谷歌搜索 - 问题是谷歌不太擅长搜索计算机语法问题,因为它删除了标点符号,并且因为需要javascript语法等类似于蹩脚(过度广泛)的搜索条件。

And yes, I have been Googling--the problem is that Google isn't great at searching for computer syntax questions since it strips punctuation, and because "require javascript syntax" and the like make for crappy (over-broad) search terms.

推荐答案


  1. require([...],function(args){} 语法是说加载这个模块列表,一旦它们全部被加载,就调用这个函数,并将这些模块的返回值作为参数。方括号中的内容是脚本文件的路径数组(减去.js)或使用 require.config paths部分。回调函数的参数对应于数组中的路径/模块,但并非所有模块都返回有用的值,正如您在下一个问题中所注意到的那样......

  1. The require([...], function(args) { } syntax is saying "load this list of modules, and once they're all loaded, call this function with the return values of those modules as arguments". What goes in the square brackets is an array of either paths to a script file (minus the .js) or a module IDs mapped using the require.config paths section. The arguments to the callback function correspond to the paths/modules in the array, but not all modules return a useful value, as you noticed in your next question...

为FeatureLayer添加参数不起作用,因为您无法跳过参数。但请注意,许多模块实际上并未返回要使用的值。你会看到很多jQuery插件,其中模块的加载只是用jQuery注册插件但不向调用者返回值。我不知道ESRI,但是从代码片段看起来加载FeatureLayer只是将FeatureLayer添加到esri.layers全局对象。

Adding an argument for FeatureLayer doesn't work because you can't skip arguments. But note that many modules don't actually return a value intended to be used. You will see this a lot with jQuery plugins, where the loading of the module simply registers the plugin with jQuery but doesn't return a value to the caller. I don't know ESRI, but from the code snippet it looks like loading FeatureLayer simply adds FeatureLayer to the esri.layers global object.

感叹号语法保留给插件。通常,在感叹号之后会有其他东西指示插件将加载的资源,例如, text!myTemplate.html ,但是在 domReady!的情况下,插件的存在只是一种等待DOM的方式在调用你的回调函数之前加载,所以没有什么需要跟随感叹号。

The exclamation point syntax is reserved for plugins. Typically there would be something else following the exclamation point which indicates the resource that the plugin would load, e.g. text!myTemplate.html, but in the case of domReady! the plugin exists just as a way to wait until DOM loaded before invoking your callback function, so nothing needs to follow the exclamation point.

外部资源的推荐列表对于StackOverflow是偏离主题的,但是这里有一个我发现有助于获得基本概念: http://aaronhardy.com/javascript / javascript-architecture-requirejs-dependency-management /

Recommendation lists of external resources is off-topic for StackOverflow, but here's one that I found helpful in getting the basic concepts: http://aaronhardy.com/javascript/javascript-architecture-requirejs-dependency-management/

这篇关于有人可以解释这个require.js示例中涉及的语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆