我有一个关于如何在嵌套函数场景中处理"this"指针的问题.
假设我将以下示例代码插入到网页中.当我调用嵌套函数"doSomeEffects()"时出错.我检查了Firebug,它表明当我在嵌套函数中时,"this"指针实际上指向全局"窗口"对象 - 我没想到.我一定不能理解正确的东西,因为我认为既然我在对象的函数中声明了嵌套函数,它应该具有与函数相关的"局部"范围(即"this"指针将指向对象本身就像它是如何在我的第一个"如果"声明中.
任何指针(没有双关语意)将不胜感激.
var std_obj = {
options : { rows: 0, cols: 0 },
activeEffect : "none",
displayMe : function() {
// the 'this' pointer is referring to the std_obj
if (this.activeEffect=="fade") { }
var doSomeEffects = function() {
// the 'this' pointer is referring to the window obj, why?
if (this.activeEffect=="fade") { }
}
doSomeEffects();
}
};
std_obj.displayMe();
Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多与我要提出的问题非常相似的帖子,但我只是想确保没有更复杂的方法来做到这一点.任何反馈都非常感谢.
我想创建一种机制来检查登录用户是否可以访问当前正在调用的php脚本.如果是这样,脚本将继续; 如果没有,脚本就会失败,使用类似的东西die('you have no access').
我想出了两种方法来完成这个:
(请假设我的会话内容已编码/工作正常 - 即我调用session_start(),正确设置会话变量等)
首先定义一个全局变量,然后检查所需头文件中的全局变量.例如:
current_executing_script.php的内容:
// the role the logged in user must have to continue on
$roleNeedToAccessThisFile = 'r';
require 'checkRole.php''
Run Code Online (Sandbox Code Playgroud)
checkRole.php的内容:
if ($_SESSION['user_role'] != $roleNeedToAccessThisFile) die('no access for you');
Run Code Online (Sandbox Code Playgroud)在头文件中定义一个函数,并在包含/要求后立即调用该函数:
checkRole.php的内容:
function checkRole($roleTheUserNeedsToAccessTheFile) {
return ($_SESSION['user_role'] == $roleTheUserNeedsToAccessTheFile);
}Run Code Online (Sandbox Code Playgroud)
current_executing_script.php的内容:
require 'checkRole.php';
checkRole('r') or die('no access for you');Run Code Online (Sandbox Code Playgroud)我想知道是否有一种方法基本上只是将参数传递给checkRole.php作为include或require构造的一部分?
提前致谢.
我一直想知道如何决定在使用服务器端代码与客户端代码之间进行选择来构建HTML页面.我将使用一个非常简单的php vs javascript/jquery示例来进一步解释我的问题.非常感谢您的建议和意见.
假设我要向用户提供一个网页,以便在我的网页中选择一种报告.哪个更有意义?
对于服务器端创建,我会这样做:
<div id="reportChoices">
<?php
// filename: reportScreen.php
// just for the sake of simplicity, say a database returns the following rows
// that indicates the type of reports that are available:
$results = array(
array("htmlID"=>"battingaverage", "htmlLabel"=>"Batting AVG report"),
array("htmlID"=>"homeruntotals", "htmlLabel"=>"Home Run Totals report"),
);
foreach ($results AS $data)
echo "<input type='radio' name='reportType' value='{$data['htmlID']}'/>{$data['htmlLabel']}";
?>
</div>
Run Code Online (Sandbox Code Playgroud)
使用客户端代码,我将获得javascript来构建如下所示的页面:
<!-- filename: reportScreen.html -->
<div id="reportChoices">
</div>
<!-- I could put this in the document.ready handler, of course -->
<script …Run Code Online (Sandbox Code Playgroud) 这个问题可能有一个简单的答案,但对于我的生活,我不明白为什么以下两种使用$ .add()的方法给了我不同的结果集.
var toBeSorted;
if (contextEl.hasClass("lv"))
toBeSorted = $(".lv", contextEl).add(contextEl);
else
toBeSorted = $(".lv", contextEl);
Run Code Online (Sandbox Code Playgroud)
与
var toBeSorted = $(".lv", contextEl);
if (contextEl.hasClass("lv"))
toBeSorted.add(contextEl);
Run Code Online (Sandbox Code Playgroud)
当IF语句为真时,我总是在顶部代码段中获得一个元素,而不是在底部代码段中(即contextEl在结果集中 - 这正是我想要的).我不明白为什么底部方法调用toBeorted.add(contextEl)的方法不起作用?
非常感谢任何指针或建议.
谢谢!
php ×2
add ×1
client-side ×1
function ×1
include ×1
javascript ×1
jquery ×1
json ×1
nested ×1
parameters ×1
require ×1
selector ×1
server-side ×1
this ×1