小编the*_*eye的帖子

如何在node.js中实际使用Q promise?

这可能是一个noob问题,但我是承诺的新手,并试图找出如何在node.js中使用Q.

我看到教程以a开头

promiseMeSomething()
    .then(function (value) {}, function (reason) {});
Run Code Online (Sandbox Code Playgroud)

但是我没有意识到它究竟.then来自哪里.我想它来自

var outputPromise = getInputPromise()
    .then(function (input) {}, function (reason) {});
Run Code Online (Sandbox Code Playgroud)

但是哪里getInputPromise()来的?我发现以前没有提到它.


我已将它包含在我的项目中

var Q = require('q');

// this is suppose, the async function I want to use promise for
function async(cb) {
    setTimeout(function () {
        cb();
    }, 5000);
}

async(function () {
    console.log('async called back');
});
Run Code Online (Sandbox Code Playgroud)

我如何在我的例子中使用Q.then

javascript node.js promise q

33
推荐指数
1
解决办法
3万
查看次数

我真的需要在java中定义默认构造函数吗?

它在未定义构造函数时工作正常,但如果我定义参数化构造函数而不是默认构造函数并且在创建对象时不传递任何值,则会出错.我认为构造函数是预定义的.

如果我已经定义了参数化构造函数,为什么还需要定义默认构造函数?是不是预定义的默认构造函数?

java constructor default-constructor

32
推荐指数
3
解决办法
4万
查看次数

如何用装饰器类装饰实例方法?

考虑这个小例子:

import datetime as dt

class Timed(object):
    def __init__(self, f):
        self.func = f

    def __call__(self, *args, **kwargs):
        start = dt.datetime.now()
        ret = self.func(*args, **kwargs)
        time = dt.datetime.now() - start
        ret["time"] = time
        return ret

class Test(object):
    def __init__(self):
        super(Test, self).__init__()

    @Timed
    def decorated(self, *args, **kwargs):
        print(self)
        print(args)
        print(kwargs)
        return dict()

    def call_deco(self):
        self.decorated("Hello", world="World")

if __name__ == "__main__":
    t = Test()
    ret = t.call_deco()
Run Code Online (Sandbox Code Playgroud)

打印

Hello
()
{'world': 'World'}
Run Code Online (Sandbox Code Playgroud)

为什么self参数(应该是Test obj实例)不作为第一个参数传递给装饰函数decorated

如果我手动完成,例如:

def call_deco(self):
    self.decorated(self, "Hello", …
Run Code Online (Sandbox Code Playgroud)

python class self python-decorators

32
推荐指数
2
解决办法
1万
查看次数

'悬挂'JavaScript变量

我不完全理解为什么以下显示"悬挂"到最后.

var x = 'set';
var y = function () 
{
    // WHAT YOU DON'T SEE -> var x; 
    // is effectively "hoisted" to this line!

    if (!x) 
    { 
        // You might expect the variable to be populated at this point...it is not
        // though, so this block executes
        var x = 'hoisted'; 
    }

    alert(x); 
}

//... and this call causes an alert to display "hoisted"
y();
Run Code Online (Sandbox Code Playgroud)

任何指针将不胜感激.

javascript hoisting

31
推荐指数
2
解决办法
2720
查看次数

从子类调用超类函数的正确方法

我有一个"SuperClass","info"作为实例变量."SuperClass"具有"printInfo()"功能."printInfo()"需要访问实例变量"info".我想创建一个"分类"里面也有方法"printInfo()".我想从"子类"的"printInfo()"称之为"超类"的printInfo().

SuperClass = function()
{
    this.info = "I am superclass";
    console.log("SuperClass:");
};

SuperClass.prototype.printInfo = function(that)
{
    console.log("printing from superclass printInfo");
    console.log(that.info);
};

SubClass = function(){};

SubClass.prototype = new SuperClass();

SubClass.prototype.printInfo = function()
{
    console.log("calling superclass");
    this.constructor.prototype.printInfo(this);
    console.log("called superclass");
};

var sc = new SubClass();
sc.printInfo();
Run Code Online (Sandbox Code Playgroud)

你可以看到,我路过""作为一个参数printInfo.如果没有"说"参数,"信息"打印为"不确定".就像在以下情况下,"this.info"当此功能从"子类"的对象调用是不确定的.

SuperClass.prototype.printInfo = function()
    {
        console.log("printing from superclass printInfo");
        console.log(this.info);
    };
Run Code Online (Sandbox Code Playgroud)

什么是覆盖并调用在JavaScript超类的方法,使函数访问类的实例变量的正确方法?

javascript inheritance prototypal-inheritance

30
推荐指数
5
解决办法
4万
查看次数

如何从字符串中删除特定单词?

我需要从字符串中删除特定的单词.

但我发现python strip方法似乎无法识别一个有序的单词.刚剥离传递给参数的任何字符.

例如:

>>> papa = "papa is a good man"
>>> app = "app is important"
>>> papa.lstrip('papa')
" is a good man"
>>> app.lstrip('papa')
" is important"
Run Code Online (Sandbox Code Playgroud)

我怎么能用python剥离一个指定的单词?

python string strip

30
推荐指数
4
解决办法
11万
查看次数

为什么在变量名后添加一个尾随逗号使其成为一个元组?

我想知道为什么在变量名后添加一个尾随逗号(在这种情况下是一个字符串)使它成为一个tuple.即

>>> abc = 'mystring',
>>> print(abc)
('mystring',)
Run Code Online (Sandbox Code Playgroud)

当我打印abc它返回tuple ('mystring',).

python syntax tuples

29
推荐指数
3
解决办法
2万
查看次数

Javascript:返回字符串中的最后一个单词

对它的权利:

我有一个words字符串,其中有两个单词,我需要返回最后一个单词.他们被""分开.我该怎么做呢?

function test(words) {

var n = words.indexOf(" ");
var res = words.substring(n+1,-1);
return res;

}
Run Code Online (Sandbox Code Playgroud)

我被告知要使用indexOf,substring但不是必需的.任何人有一个简单的方法来做到这一点?(有或没有indexOfsubstring)

javascript string substring

29
推荐指数
4
解决办法
5万
查看次数

GHCi"让" - 它做什么?

我很欣赏有人可以指出关于GHCi中"let"做什么的文档,或者说失败,有说服力地解释它:-).

据我所知,"let"(没有"in")本身不是Haskell语言的一部分,另一方面,它似乎也不是GHCI命令,因为它没有以冒号为前缀.

haskell let ghci

28
推荐指数
3
解决办法
3821
查看次数

布尔数组在O(1)空间和O(n)时间内重新排序

问题取自编程访谈要素:

给定具有布尔值键的n个对象的数组A,对数组重新排序,以便首先出现具有错误键的对象.具有键true的对象的相对排序不应更改.使用O(1)额外空间和O(n)时间.

我做了以下操作,它保留了对象为true的对象的相对排序,并使用了O(1)额外空间,但我相信它的时间复杂度为O(n*n!).

public static void rearrangeVariant4(Boolean[] a) {
  int lastFalseIdx = 0;
  for (int i = 0; i < a.length; i++) {
    if (a[i].equals(false)) {
      int falseIdx = i;
      while (falseIdx > lastFalseIdx) {
        swap(a, falseIdx, falseIdx-1);
        falseIdx--;
      }
      lastFalseIdx++;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

任何人都知道如何在O(n)时间内解决它?

java arrays algorithm

27
推荐指数
2
解决办法
2111
查看次数