今天我正在研究一个使用链式函数调用的宠物项目,我很好奇我如何检测链中的最后一个函数何时被执行.例如:
func1('initial data').func2().func3().func4();
Run Code Online (Sandbox Code Playgroud)
在func2-4完成"初始数据"处理之后,我想检测func4何时完成.因为func4()并不总是链中的最后一个函数,例如它可以以.func3()或.func5()结尾,或者我可以根据我想要做的事情混合我的函数调用,我试图想办法检测不再有函数调用,但我没有走得太远.
public class Parent
{
public virtual Parent me()
{
return this;
}
}
public class Child : Parent
{
}
Run Code Online (Sandbox Code Playgroud)
new Child().me()返回一个Parent对象.我需要让它返回Child对象本身(不使用扩展和泛型)?
我有一些类,我喜欢链式方法,以提供流畅的配置风格.
例如
public class BaseFoo
{
private bool _myProp = false;
public [[[BaseFoo?]]] SetMyProperty()
{
this._myProp = true;
return this;
}
}
public class DerivedFoo : BaseFoo
{
public bool _derivedProp = false;
public DerivedFoo SetDerivedPropery()
{
this._derivedProp = true;
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用base方法返回BaseFoo类型时,问题显然是在尝试将它们链接在一起时.显然我可以将它转换回DerivedFoo,但是有一种简单的方法可以返回派生类类型的对象.
我能想到的唯一方法就是将构造函数链接在一起并将父类型传递给初始构造函数,但需要使用语法.
另一种方法是为每个子类提供类似的代理方法,但返回派生类型.
DerivedFoo foo = new DerivedFoo();
foo.SetMyProperty().SetDerivedPropery(); // wont work because the SetMyProperty returns type of BaseFoo
foo.SetDerivedPropery().SetMyProperty(); // works because I know i'm calling the method of the derived class first
(foo.SetMyProperty() as …Run Code Online (Sandbox Code Playgroud) 我希望能够直接从函数的返回值访问数组.
e.g.
$arr = find_student();
echo $arr['name'];
// I want to be able to do
echo find_student()['name']
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?没有另一行代码?
每当检查顶级单选按钮时,我都试图清除表单元素上的一堆错误样式.我试图找到:
用于查找所有表单元素的链接方法,因此我没有进行如此多的调用..(不确定是否可能).
//在调用下面的函数之前定义的全局变量OR范围变量.
JS
var target = jQuery("#cachedElement");
function functionWithManyReferencesToTarget() {
target.find("input").each(function() {
$(this).removeClass("formError error-state");
});
target.find("label").each(function() {
$(this).removeClass("formError error-state");
});
target.find("select").each(function() {
$(this).removeClass("formError error-state");
});
}
Run Code Online (Sandbox Code Playgroud) 我已经看到很多关于这个的问题,但似乎没有什么能为我的案子提供正确的答案.我也看到了使用的答案,.pipe但我正在寻找一个使用的答案.then.
好的.我需要做3个ajax调用,让我们说一个允许多个帐户的民意调查应用程序.需要执行以便帐户可以投票的过程如下.
假设我有两个帐户:
var accts = [{user: "acct1", pswd: "1234"},{user: "acct2", pswd: "4321"}];
Run Code Online (Sandbox Code Playgroud)
现在我需要使用jquery来遍历这些帐户 $.each
$.each(accts, function(key,value){
});
Run Code Online (Sandbox Code Playgroud)
我了解到使用$.Deferred可以完美地做到这一点,但是正确的实现.
我想要的是
--------loop1--------
login
select
vote
--------loop2--------
login
select
vote
All Done!.
Run Code Online (Sandbox Code Playgroud)
但是会发生什么(当我尝试console.log发生的事情时)全部完成!登录(2)选择(2)投票(2)
所以这是我的代码:
$.each(data, function(k, v) {
promise.then(function() {
return $.post(loginURL, {user: v.username, passwrd: v.password});
}).then(function(html) {
if (data > 0) {
console.log('Logged In!');
return $.post(pollURL + 'select.php', {id: 143});
} else {
console.log('Login Failed.');
return false;
} …Run Code Online (Sandbox Code Playgroud) 我不确定"链接"是否是正确的术语,但我要问的是它是否有可能使PDO查询类似于这个MySQLi查询...
$sql = mysqli_fetch_object($db->query("SELECT username FROM member WHERE userID = 1");
Run Code Online (Sandbox Code Playgroud)
使用PDO我只能这样做
$sql = $db->query("SELECT username FROM member WHERE userID = 1");
$query = $sql->fetch(PDO::FETCH_OBJ);
Run Code Online (Sandbox Code Playgroud)
是否有可能"链接"(如果有更好的术语,请纠正我)PDO的查询或否?
我目前在我面前有一个非常棒的if-then-else循环,它首先从web服务获得多个cantinas.然后它会获得所有可用餐点(每个菜单)和所有可用的配菜(每个菜单).对于每一餐和两餐,然后检查所有添加剂并聚集它们.
最后,我有多种菜单,包括餐点,配菜和所有添加剂.
以下流程图显示了该过程:

我想美化代码,并希望使用jQuery的promises - 但我无法弄清楚如何做到这一点,因为我必须在a then之后堆叠when(至少我认为,也许我必须resolve?).以下是我最好的尝试:
//this totally does not work at all, but you get the idea what I want to do
Menus.getCantinas()
.when(Menus.getMeals(cantinas), Menus.getSides(cantinas)) //when doesn't exist here, neither does cantinas
.then(Menus.getAdditives(meals, sides) //also throws errors as meals and sides does not exist
.done(function(cantinas, meals, sides, additives) { //more errors for the people
Menus.cantinas = cantinas;
Menus.meals = meals;
Menus.sides = sides;
Menus.additives = additives;
//... some html stuff to build …Run Code Online (Sandbox Code Playgroud) 使用方法链时的返回类型是什么?any似乎太通用了.
<cffunction name="get" output="false" returntype="any">
<cfargument name="BossID" type="string" required="false" default="All">
...
<cfreturn this>
</cffunction>
Run Code Online (Sandbox Code Playgroud) 我遇到了以下代码行:
getLogger().info("Text Goes Here"); // This command outputs the text to console
Run Code Online (Sandbox Code Playgroud)
我理解对象如何工作和被调用的基础知识,但在我在youtube上观看的视频中,vivz(作者),从未解释过在方法中调用方法(至少我认为在上面的代码中发生了这种情况).
他解释说
ClassName.variable
xVariable.method()
Run Code Online (Sandbox Code Playgroud)
但没有任何关系
someMethod().anotherMethod();
Run Code Online (Sandbox Code Playgroud)
在初学者的术语中,任何人都可以解释这个概念或对这里发生的事情的一些解释吗?
基本上,我想知道,如果info()是一个方法里面getLogger(),或者是什么getLogger(),并info()在这种情况下?
chaining ×10
javascript ×3
jquery ×3
c# ×2
methods ×2
php ×2
promise ×2
ajax ×1
bukkit ×1
coldfusion ×1
deferred ×1
fluent ×1
function ×1
inheritance ×1
java ×1
mysqli ×1
pdo ×1
performance ×1
polymorphism ×1