是否可以使用call methodjavascript(如[mdn documentation]中所述)来传递参数this?
有这样的代码:
console.log(this);
$('#image_id').load(function () {
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)
我希望第二个this(包含在其中的那个load function)与第一个相同.
我试过了
console.log(this);
$('#image_id').load.call(this, function () {
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
提前感谢大家的任何建议.
我的代码也可以在这里找到.
这段代码可行(但代码重复很多):
Employee::Employee(const Employee& x)
{
name=new char [strlen(x.name)+1];
strcpy(name, x. name);
strcpy(EGN, x.EGN);
salary=x.salary;
}
void Employee::operator=(const Employee& x)
{
delete[] name;
name=new char [strlen(x.name)+1];
strcpy(name, x. name);
strcpy(EGN, x.EGN);
salary=x.salary;
}
Employee::Employee(char* n, char* e, double s)
{
name = new char [strlen(n)+1];
strcpy(name, n);
strcpy(EGN, e);
salary=s;
}
Run Code Online (Sandbox Code Playgroud)
以下是我试图避免三次写同样的事情......但它不起作用.是不是可以缩短代码?
Employee::Employee(char* n, char* e, double s)
{
name = new char [strlen(n)+1];
strcpy(name, n);
strcpy(EGN, e);
salary=s;
}
Employee::Employee(const Employee& x)
{
Employee(x.name, x.EGN, x.salary);
} …Run Code Online (Sandbox Code Playgroud) 我有一个非常困惑的问题,即在文件的顶部,我调用了一个外部函数“ sessionTest”,该函数测试各种会话条件并返回正确的头字符串。这是必要的,因为我有两个不同的标题:一个用于登录的用户,一个用于非用户/注销的用户。
我的问题在以下代码中:
<?php include './session.php';
include './db.php';
$con = genCon();
if(isset($_SESSION['logged'])){
echo "Logged: ".$_SESSION['logged'];
}
$headerstring = sessionTest($con); ?>
...Rest of Page...
Run Code Online (Sandbox Code Playgroud)
由于某种原因,isset($ _ SESSION ['logged'])在这里返回false,但在sessionTest中为true:
function sessionTest($con){
session_start();
$fingerprint = md5($_SERVER['HTTP_USER_AGENT'].session_id());
if(isset($_SESSION['user']) && isset($_SESSION['logged']) && $_SESSION['logged']==TRUE){
$dbFingerprint = qGetUserFingerprint($_SESSION['user'],$con);
$fpMatch = ($dbFingerprint == $fingerprint);
//LEAVING OUT NON RELEVANT CODE
$headerstring = './lheader.html';
}else{
$headerstring = './header.html';
}
return $headerstring;
}
Run Code Online (Sandbox Code Playgroud)
标头字符串设置为lheader.html,因此isset($ _ SESSION ['logged'])在此处返回true。有谁知道为什么?
提前致谢!
我快要绝望了!我需要从指向我的基类的指针调用非虚拟成员函数:
class A { };
class B : public A { public: void evilFunction() { std::cout << "Yay!"; } };
int main(void) {
A *pointer = new B();
// Now do something like this:
// pointer->evilFunction();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以用dynamic_cast做到这一点 - 但我不被允许!我真的不知道我还能做些什么.从理论上讲,由于我有指针,我很确定我可以用指针算术做一些魔法来获取函数的内存位置然后调用它,但我不知道怎么做或者至少如何开始.
谁能给我一个提示?这就是我所需要的一切.或者我将使用我的史诗初学者技能编写代码来删除g ++以报复C++对我造成的痛苦!你不能让这种情况发生,对吧?!
有时需要在一个命令中进行多次调用.一个简单的例子可能是strrep.假设您要用括号替换所有括号,所有逗号用点替换,然后删除所有双引号.然后可能需要以下伪代码:
strrep(myString, '()', '[]', ',', '.', '"', '')
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?你当然可以选择:
strrep(strrep(strrep(myString, '()', '[]'), ',', '.'), '"', '')
Run Code Online (Sandbox Code Playgroud)
或者将字符串保存在单元格数组中并在for循环中使用它,但这两种解决方案都非常难看.
最理想的答案是对所有以类似方式工作的函数都是通用的.
$\ = "\n";
sub foo
{
print("one");
}
foo(); // mark1
sub foo
{
print("two");
}
foo(); //mark2
Run Code Online (Sandbox Code Playgroud)
执行上面的代码时,输出将是:两个,两个.据我所知Perl是一个解释器,所以当调用foo(mark1)时,不应该首先打印,而当调用foo()(mark2)时必须打印两个.但是为什么两个时间都打印两个,请解释一下怎么样?
基本上,我要做的是更改函数内的struct变量.这是代码:
int weapon_equip(struct player inventory, int in) {
int x = in - 1, y;
int previous_wep[4];
//Stores the values of the previous equipped weapon.
for(y = 0; y < 4; y++)
previous_wep[y] = inventory.weapons[0][y];
/* Since the equipped weapon has a first value of 0,
I check if the player hasn't chosen a non-existant
item, or that he tries to equip the weapon again.*/
if(inventory.weapons[x][TYPE] != NULL && x > 0) {
inventory.weapons[0][TYPE] = inventory.weapons[x][TYPE];
inventory.weapons[0][MATERIAL] = inventory.weapons[x][MATERIAL];
inventory.weapons[0][ITEM] …Run Code Online (Sandbox Code Playgroud) 我有一个调用函数的Perl程序,在这种情况下ref,检查结果.具体来说,我正在测试一个变量是一个哈希引用.在那种情况下,ref将返回'HASH'.我测试了它,它工作.
然后我决定记录它,添加一个print显示相同调用的结果,但它无法正常工作.这是一个简化版本:
use strict;
use warnings;
my $book_ref = {};
$book_ref->{'title'} = 'The Lord of the Rings';
if (ref $book_ref eq 'HASH') {
print "ref \$book_ref is a " . ref $book_ref . "\n";
}
print "Program is over\n";
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,这是输出:
ref $book_ref is a Program is over
Run Code Online (Sandbox Code Playgroud)
尽管使用strict并且warnings既没有错误也没有警告.
调用ref是完全相同的(它是复制和粘贴),但它在if条件内正常工作,print不显示任何内容,实际上似乎被中断,因为明确跳过了换行符.为什么行为改变了?
表达式的增量是否x++总是在变量复制到函数之后进行?
电话:
foo(x++, y);
Run Code Online (Sandbox Code Playgroud)
功能 :
int foo(int x, int y)
{
return x*y;
}
Run Code Online (Sandbox Code Playgroud)
这是所有编译器的未定义行为吗?
我正在写一个小的Haskell练习,应该在列表中移动一些元素,类似于Caesar密码,代码已经在工作,下面是代码。
module Lib (shift, cipherEncode, cipherDecode ) where
import Data.Char
import Data.List
import Data.Maybe
abcdata :: [Char]
abcdata = ['a','b','c','d','e','f','g']
iabcdata :: [Char]
iabcdata = ['g','f','e','d','c','b','a']
shift :: Char -> Int -> Char
shift l n = if (n >= 0)
then normalShift l n
else inverseShift l (abs n)
normalShift :: Char -> Int -> Char
normalShift l n = shifter l n abcdata
inverseShift :: Char -> Int -> Char
inverseShift l n = shifter l n reverse(abcdata) …Run Code Online (Sandbox Code Playgroud) function-call ×10
c ×2
c++ ×2
perl ×2
class ×1
function ×1
haskell ×1
increment ×1
isset ×1
javascript ×1
jquery ×1
matlab ×1
method-call ×1
oop ×1
php ×1
pointers ×1
return-value ×1
session ×1
syntax ×1