开发者

needs for synchronous programming

EDIT: This question was misexpressed. What I'开发者_开发问答ve really wanted to ask was:

Is there anything what cant be written in OO languages (with support for closures) using continuation-passing style?

You can google what CPS does mean or just stick with definition of function/method never returning anything, always pushing data somewhere - using passed callback.

And after yers from original question, I can even answer myself - there's nothing like that. And moreover it's actually very good OO principle called Tell Dont Ask

function getName(){
  return this.name;
}

console.log(xyz.getName())

vs.

function pushNameTo(callback){
  callback(this.name);
}

xyz.pushNameTo(console.log)

good, but this time it was named after how it does the thing, lets name it after what it does and make it even more OO:

function renderOn(responseBuilder){
  var b = responseBuilder;

  //or just string, whatever, depending on your builder implementation
  b.field("Name: ", this.name);
  b.field("Age: ", this.age);
  b.image("Profile photo", this.imageData);
}

person.renderOn(htmlBuilder);

the point here is - the object encapsulates not only its data but even behavior, the spirit, personality. Who else should be responsible for expressing person's representation rather than person itself?

Of course this does not necessarily means you should have html in your code, builder serves this purpose. It can even generate some xml or other data-format for actual UI-rendering layer. But its always push instead of pull.


Nothing, of course. Consider: if you have a program that is completely sequential, you could simply insert it into some kind of wrapper, like document.onload(). Then the sequential program would be started asynchronously.

Going the other way around, if all you have is a synchronous language, you can always write the asynchronous case by having a table of pieces to be executed, and an inner loop that looks to see what's been enabled, and takes it from the table to execute. in fact, this would look very much like the underlying runtime in whoich your javascript runs.


There are two types of programs -- imperative and functional.

Imperative programs are sequantial -- one step after another. C++, Java, etc. are examples.

Functional programs may not be sequential. Most async patterns use "continuation-style" programming, which is a type of functional programming with imperative overtones.

JavaScript is an imperative language which has first-class functions, i.e. it also enables certain functional programming paradigms.

What you described in your question is "continuation-style" async programming. Notice that the meaning of a "continuation" is "the rest of the program after this line". Therefore, theoretically, every imperative program can be rewritten in "continuation" style (i.e. the first line with a continuation of the rest of the program starting form the second line, and so on and so forth). For example:

Statement #1
Statement #2
Statement #3

can be rewritten as:

do(Statement #1, function{
  do(Statement #2, function{
    Statement #3
  })
})

where the second parameter to do is the continuation of the statement.

Loops are more tricky though, but they can also be rewritten similarly -- essentially passing the loop body itsslef as the continuation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜