以点开头的行是什么意思?

Jef*_*unk 4 javascript

我正在阅读Crafty教程并遇到了一个我无法找到文档的代码片段.搜索标点很难.

有问题的行,11和12,遵循这Crafty.e条线,并以.text和开头.css.这些属性属于哪个对象?

//the loading screen that will display while our assets load
Crafty.scene("loading", function () {
    //load takes an array of assets and a callback when complete
    Crafty.load(["sprite.png"], function () {
        Crafty.scene("main"); //when everything is loaded, run the main scene
    });

    //black background with some loading text
    Crafty.background("#000");
    Crafty.e("2D, DOM, Text").attr({ w: 100, h: 20, x: 150, y: 120 })
            .text("Loading")
            .css({ "text-align": "center" });
});

//automatically play the loading scene
Crafty.scene("loading");
Run Code Online (Sandbox Code Playgroud)

这在规范中会在哪里?

Jes*_*sse 5

以a开头的行.只是在前一个函数/行的对象上调用的函数/属性.


在您的具体情况下,

Crafty.e("2D, DOM, Text").attr({ w: 100, h: 20, x: 150, y: 120 })
      .text("Loading")
      .css({ "text-align": "center" });
Run Code Online (Sandbox Code Playgroud)

.text("Loading")只是函数调用的结果Crafty.e(...).

同样,.css({ "text-align": "center" })只是对前一行结果的函数调用.text("Loading").

因为它在同一行,所以.attr(...)调用不是外观可见的,但它与其他行中的调用完全相同.


在扩展术语中,上面的示例与执行此操作相同:

var eResult = Crafty.e("2D, DOM, Text");
var attrResult = eResult.attr({ w: 100, h: 20, x: 150, y: 120 });
var textResult = attrResult.text("Loading");
var cssResult = textResult.css({ "text-align": "center" });
Run Code Online (Sandbox Code Playgroud)

正如其他人所说,这只是一种将调用链接到同一个对象的方法 - 但是,请注意(!)在所有编程语言中并不总是这样.jQuery和许多其他JavaScript框架/库已经采用这种方法使开发更容易/更顺畅,因此,它在JavaScript开发中很普遍.

特别是在JavaScript中,真正的语句终止是;(分号).这意味着单个语句可以跨越多行.