标签: chaining

方法链中的C++执行顺序

该程序的输出:

#include <iostream> 
class c1
{   
  public:
    c1& meth1(int* ar) {
      std::cout << "method 1" << std::endl;
      *ar = 1;
      return *this;
    }
    void meth2(int ar)
    {
      std::cout << "method 2:"<< ar << std::endl;
    }
};

int main()
{
  c1 c;
  int nu = 0;
  c.meth1(&nu).meth2(nu);
}
Run Code Online (Sandbox Code Playgroud)

方法是:

method 1
method 2:0
Run Code Online (Sandbox Code Playgroud)

开始nu时为什么不是1 meth2()

c++ operator-precedence chaining

105
推荐指数
4
解决办法
7411
查看次数

Javascript继承:调用超级构造函数还是使用原型链?

最近我读到了MDC中的JavaScript调用用法

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call

如下所示的一个例子,我仍然不明白.

他们为什么在这里使用继承呢?

Prod_dept.prototype = new Product();
Run Code Online (Sandbox Code Playgroud)

这有必要吗?因为有一个超级构造函数的调用

Prod_dept()
Run Code Online (Sandbox Code Playgroud)

无论如何,像这样

Product.call
Run Code Online (Sandbox Code Playgroud)

这只是出于常见的行为吗?何时使用超级构造函数调用或使用原型链更好?

function Product(name, value){
  this.name = name;
  if(value >= 1000)
    this.value = 999;
  else
    this.value = value;
}

function Prod_dept(name, value, dept){
  this.dept = dept;
  Product.call(this, name, value);
}

Prod_dept.prototype = new Product();

// since 5 is less than 1000, value is set
cheese = new Prod_dept("feta", 5, "food");

// since 5000 is above 1000, value will be 999
car = new Prod_dept("honda", 5000, "auto");
Run Code Online (Sandbox Code Playgroud)

谢谢你让事情更清楚

javascript inheritance call chaining prototype-programming

78
推荐指数
3
解决办法
8万
查看次数

如何选择元素的父元素和父元素的兄弟元素

我有这个代码:

$("#test").siblings('p').remove();
$("#test").remove();
Run Code Online (Sandbox Code Playgroud)

如何链接此代码而不是单独编写代码?

jquery css-selectors jquery-selectors chaining removeall

54
推荐指数
4
解决办法
3071
查看次数

搜索元素的后代

量角器什么是选择子元素的最佳方式?假设我们有以下布局......

<div id='parent_1'>
    <div class='red'>Red</div>
    <div class='blue'>Blue</div>
</div>
<div id='parent_2'>
    <div class='red'>Red</div>
    <div class='blue'>Blue</div>
</div>
Run Code Online (Sandbox Code Playgroud)

使用jQuery,我们会做这样的事情.

var p1 = $('#parent_1');
var p1_red = $('.red', p1);  //or p1.find('.red');
var p1_blue = $('.blue', p1); //or p1.find('.blue');
Run Code Online (Sandbox Code Playgroud)

但是使用Protractor,首先获得父元素是否有意义?因为这样做var p1 = element('#parent_1');实际上不会检索/搜索对象,直到getText()调用它为止.

这样做..

场景1

expect(p1.element('.red')).toBe('red');
expect(p1.element('.blue')).toBe('blue');
Run Code Online (Sandbox Code Playgroud)

要么

情景2

expect(element('#parent_1').element('.red')).toBe('red');
expect(element('#parent_1').element('.blue')).toBe('blue');
Run Code Online (Sandbox Code Playgroud)

要么

场景3

expect(element('#parent_1 > .red')).toBe('red');
expect(element('#parent_1 > .blue')).toBe('blue');
Run Code Online (Sandbox Code Playgroud)

一种方法相对于另一种方法有什么好处吗?

这就是我正在做的事情,但我不知道将父母与cssSelector分开是否有任何好处:

function getChild(cssSelector, parentElement){
    return parentElement.$(cssSelector);
}

var parent = $('#parent_1');
var child_red = getChild('.red', parent);
var child_blue = getChild('.blue', parent); …
Run Code Online (Sandbox Code Playgroud)

element parent-child chaining protractor

46
推荐指数
1
解决办法
4万
查看次数

如何在java中实现方法链接?

我想在Java中实现方法链接.

我怎样才能实现它?

也让我知道何时使用它.

public class Dialog {

     public Dialog() {

     }

     public void setTitle(String title) {

         //Logic to set title in dialog
     }

     public void setMessage(String message) {

         //Logic to set message
     }     

     public void setPositiveButton() {

         //Logic to send button
     }
}   
Run Code Online (Sandbox Code Playgroud)

我想创建方法链,我可以使用如下:

new Dialog().setTitle("Title1").setMessage("sample message").setPositiveButton();
Run Code Online (Sandbox Code Playgroud)

或者喜欢

new Dialog().setTitle("Title1").setMessage("sample message");
Run Code Online (Sandbox Code Playgroud)

或者喜欢

new Dialog().setTitle("Title1").setPositiveButton();
Run Code Online (Sandbox Code Playgroud)

java methods chaining

45
推荐指数
2
解决办法
4万
查看次数

用Java链接的方法

虽然我早些时候在这里回答了一些问题,但最近我一直在做的一些工作中,我一直在想为什么Java不支持对其内置类进行方法链接.

Car例如,如果我要创建一个类,我可以通过reutrning 而不是void来使它可链接,this如下所示:

public class Car {
    private String make;        

    public Car setMake(String make) {
        this.make = make;
        return this;
    }   
}
Run Code Online (Sandbox Code Playgroud)

内置库不倾向于以这种方式做事有什么特别的原因吗?方法链是否存在缺点?

我可能忽略了一些可以解释缺少方法链接的东西,但是默认情况下返回void的任何setter方法应该返回对此的引用(至少在我看来应该如此).这将使以下情况变得更加清洁.

container.add((new JLabel("label text")).setMaximumSize(new Dimension(100,200)));
Run Code Online (Sandbox Code Playgroud)

而不是更长时间的啰嗦:注意:如果你愿意,它不会阻止你以这种方式编码.

JLabel label = new JLabel("label text");
label.setMaximumSize(new Dimension(100,200));
container.add(label);
Run Code Online (Sandbox Code Playgroud)

我非常有兴趣听到这个决定背后的原因,如果我不得不猜测会有与此相关的开销,所以只应在需要时使用.

java methods method-chaining chaining

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

Java 8将函数应用于Stream的所有元素,而不会破坏流链

在Java中是否有办法将函数应用于a的所有元素Stream而不破坏Stream链?我知道我可以打电话forEach,但那个方法会返回一个void,而不是一个Stream.

java chaining java-8 java-stream

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

打字稿中的可选链接运算符

在javascript中,babel插件支持Optional Chaining Operator .

但我无法在Typescript中找到如何做到这一点.任何的想法?

javascript optional chaining typescript

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

基本对象/函数链如何在javascript中工作?

我正试图让jQuery风格的函数链接在我的脑海中.我的意思是:

var e = f1('test').f2().f3();
Run Code Online (Sandbox Code Playgroud)

我有一个例子可以工作,而另一个没有.我会发布以下内容.我总是想学习第一个原理的基本原理,以便我可以在它之上构建.到目前为止,我对链接的工作方式只有一个粗略而宽松的理解,而且我遇到了一些我无法智能排除故障的错误.

我知道的:

  1. 函数必须返回自己,又称"返回此";
  2. 可链接函数必须驻留在父函数中,也就是在jQuery中.css()是jQuery()的子方法,因此jQuery().css();
  3. 父函数应该返回自身或自身的新实例.

这个例子有效:

var one = function(num){
    this.oldnum = num;

    this.add = function(){
        this.oldnum++;
        return this;
    }

    if(this instanceof one){
        return this.one;    
    }else{
        return new one(num);    
    }
}
var test = one(1).add().add();
Run Code Online (Sandbox Code Playgroud)

但是这个没有:

var gmap = function(){

    this.add = function(){
        alert('add');

        return this;    
    }   

    if(this instanceof gmap) {
        return this.gmap;   
    } else{
        return new gmap();  
    }

}
var test = gmap.add();
Run Code Online (Sandbox Code Playgroud)

javascript methods method-chaining chaining

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

与AngularJS的链承诺

我有一个名为paymentStrategy的服务,它被注入我的控制器.

$scope.buy = function() {
  paymentStrategy.buy()
    .then(function(response) {

  }
}
Run Code Online (Sandbox Code Playgroud)

来自paymentStrategy的这种购买方法会触发需要按顺序调用的几种方法.当buy()中的所有方法都完成后,需要调用().

这可能是微不足道的,但我对角度很新.

目前,在init()方法之后直接触发buy().then().我觉得我们需要将所有这些方法放在一个promises数组中并应用$ q.all().

任何帮助或建议将不胜感激

angular.module('deps-app.payment.services', []).
  factory('paymentStrategy', function($q) {

 var deferred = $q.defer();
 var ITEM_TO_PURCHASE = "test.beer.managed";
 var promises = [];

 var handlerSuccess = function(result) {
      deferred.resolve(result);
  };

 var handlerError = function(result) {
      deferred.reject(result);
  };

 _init = function() {

     inappbilling.init(handlerSuccess, handlerError, { showLog:true }); 
     return deferred.promise;
    }

  _purchase = function() {
        inappbilling.buy(handlerSuccess, handlerError, ITEM_TO_PURCHASE);
        return deferred.promise;
  }

  _consume = function() {
        inappbilling.consumePurchase(handlerSuccess, handlerError, ITEM_TO_PURCHASE);
        return deferred.promise;
  }

return  { …
Run Code Online (Sandbox Code Playgroud)

javascript chaining promise angularjs

25
推荐指数
2
解决办法
5万
查看次数