gpt4 book ai didi

javascript - JS 和 YUI 中的这些结构是什么?

转载 作者:行者123 更新时间:2023-12-03 12:04:57 25 4
gpt4 key购买 nike

我从 here 得到了以下代码,同时在单独的 .js 中寻找从 YUI2 JavaScript 函数访问 PHP 变量的方法。文件。

查看 JavaScript,第一个语句创建 namespace ,第二个语句开始定义一个函数(名为YAHOO.MyApp)。然后var currencyRates;var userInfo;创建两个变量。

  1. 那么接下来是什么return {...}构造?

  2. 然后在里面,function(newRates) { currencyRates = newRates; } 看起来像一个函数(特别是因为 PHP 可能调用它并且 向其传递数组 $currency rates ?但总体来说是什么 initCurrencyRates: function(newRates) { currencyRates = newRates;
    }

    什么是:在那里(就像 = 用于赋值)?

  3. 那么();呢?到底?什么是something=function(){...}();构建?

有人可以解释一下控制流程吗?首先执行什么,然后执行什么,然后执行什么?

JavaScript:

YAHOO.namespace('MyApp');

YAHOO.MyApp = function() {

var currencyRates;
var userInfo;

/*
here a lot of code with event listeners and dom manipulations which uses currencyRates and userInfo variables
*/

return {
initCurrencyRates: function(newRates) { currencyRates = newRates; },
initUserInfo: function(newUserInfo) { userInfo = newUserInfo; },
}

}();

PHP:

<?php
$currencyRates = array('EUR' : 1.3245, 'GBP': 1.4322, 'RUB': 0.02334); //actually it comes from database
print '<script>YAHOO.MyApp.initCurrencyRates(' . json_encode($currencyRates) . ')</script>';

$userInfo = array('Name' => 'Jhon', 'ID' => 10); //actually it comes from database
print '<script>YAHOO.MyApp.initUserInfo(' . json_encode($userInfo) . ')</script>';

?>

最佳答案

您正在查看 Javascript 对象文字。它们有点类似于 PHP 的哈希文本。这段 JavaScript 代码

var foo = {
x: "hello",
y: "world"
}

与此 PHP 代码有点相似:

$foo = array(
"x" => "Hello",
"y" => "World"
)

关于javascript - JS 和 YUI 中的这些结构是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25225053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com