小编Dei*_*win的帖子

如何在JavaScript中获取外部"this"引用

所以我有一个类可以调用它A.对于这个类我写了一些函数,我可以像这样调用:

var a = new A();
a.getSomething();
a.putSomething();
a.delSomething();
Run Code Online (Sandbox Code Playgroud)

等等.现在我想我会把它整理一下所以它不会太乱,看起来有点像这样:

a.something.get();
a.something.put();
a.something.del();
Run Code Online (Sandbox Code Playgroud)

这就是我试图实现这一目标的方式:

A.prototype.something = {
  get: function(){...},
  put: function(){...},
  del: function(){...}
};
Run Code Online (Sandbox Code Playgroud)

但是这些函数(get,put和del)仍然需要访问A中的常见对象/函数,所以我需要引用A,但我不知道如何实现.

我找到的一个选项是这样的:

A.prototype.something = function(){
  var that = this;
  return {
    get: function(){...},
    ...
  };
}; 
Run Code Online (Sandbox Code Playgroud)

并且'that'将用于那些(get,put和del)函数而不是'this'.但这意味着我必须以这种方式调用这些函数:

a.something().get();
...
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎不太好.那么我有可能按照原计划的方式组织这些事情吗?

javascript

11
推荐指数
2
解决办法
2016
查看次数

通用方法与通配符 - 编译错误

我有一个问题(简化):

public void method(List<List<?>> list){...}
Run Code Online (Sandbox Code Playgroud)

调用时给出了一个编译错误:

method(new ArrayList<List<String>>()); // This line gives the error
Run Code Online (Sandbox Code Playgroud)

在阅读了类似的线程之后,我明白如果我将方法签名重写为:

public void method(List<? extends List<?>> list){...}
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是,为什么以下工作呢?

public <T> void method(List<List<T>> list){...}
Run Code Online (Sandbox Code Playgroud)

java generics

1
推荐指数
1
解决办法
193
查看次数

标签 统计

generics ×1

java ×1

javascript ×1