2013-04-15 3 views
-2
phantom = require 'phantom' 

phantom.create (ph) -> 
    ph.createPage (page) -> 
    page.open "http://www.google.com", (status) -> 
     console.log "opened google? ", status 
     page.evaluate (-> document.title), (result) -> 
     console.log 'Page title is ' + result 
     ph.exit() 

이 웹 사이트를 사용해 보았지만 정확하지는 않습니다. 그것은 모든 곳에서 귀환합니다. http://js2coffee.org/#coffee2jsJavascript에서이 Coffescript 코드와 동일한 것은 무엇입니까?

+0

'js2coffee'는 명시 적으로 반환 값을 중요시하지 않는다고 가정합니다 (coffeescript에 명시 적으로 값을 반환하지 않아야 함). –

답변

1
var phantom = require('phantom'); 

phantom.create(function(ph)) { 

    ph.createPage(function(page) { 

     page.open("http://www.google.com", function(status) { 

      console.log("opened google? ", status); 

      page.evaluate(function() { return document.title; }, function() { 

       console.log('Page title is ' + result); 
       ph.exit() 

      } 
     }); 
    }); 
}); 
3

업데이트 : 두 번째보기 후에, 이러한 반환 중 일부는 가짜/중복 인 것으로 보입니다. 이는 Coffeescript가 자바 스크립트에서 아무 것도 반환하지 않은 경우에도 컴파일러가 의도를 알 수없는 경우에도 함수의 마지막 명령문 결과를 항상 반환하므로 (return 키워드를 저장할 수 있음) 때문입니다. 그것은 불필요 할 수 있지만 반환 값을 아무도 사용하지 않았다면 아무런 해가 없습니다. "nothing"을 반환하는 것이 중요하다면 명시 적으로 그렇게 할 수도 있습니다.

var phantom; 

phantom = require('phantom'); 

phantom.create(function(ph) { 
    return ph.createPage(function(page) { 
    return page.open("http://www.google.com", function(status) { 
     console.log("opened google? ", status); 
     return page.evaluate((function() { 
     return document.title; 
     }), function(result) { 
     console.log('Page title is ' + result); 
     return ph.exit(); 
     }); 
    }); 
    }); 
}); 

그것은 모든 곳에서 수익을 가지고


당신은 그 결과 무엇을보고, 그것을 컴파일 할 수 있습니다.

글쎄, 거기에 정의한 모든 기능에는 하나의 복귀가 있습니다.

Coffeescript의 가장 중요한 동기 중 하나는 더 적은 상용구로 모든 콜백 함수를 작성할 수 있어야한다는 것입니다.

어느 쪽이든, 컴파일러는 "정확합니다".

관련 문제