Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CoffeeScript函数问题_Javascript_Coffeescript - Fatal编程技术网

Javascript CoffeeScript函数问题

Javascript CoffeeScript函数问题,javascript,coffeescript,Javascript,Coffeescript,我不熟悉咖啡脚本。我已经在js中编写了代码,现在我想在coffeescript中转换它,我已经尝试了很多,我也参考了。但这对我没有帮助 这是我的密码 this.$scope.callTestFuntion = function(){ this.blockingObject.render(function(dataURL){ console.log('via render'); console.log(dataURL.length); }); } t

我不熟悉咖啡脚本。我已经在
js
中编写了代码,现在我想在
coffeescript
中转换它,我已经尝试了很多,我也参考了。但这对我没有帮助

这是我的密码

this.$scope.callTestFuntion = function(){
    this.blockingObject.render(function(dataURL){
      console.log('via render');
      console.log(dataURL.length);
    });
  }
  this.$scope.blockingObject.callback=function(dataURL){
    console.log('via function');
    console.log(dataURL.length);
    this.myCroppedImage = dataURL;
  }

  var handleFileSelect=function(evt) {
    console.log('here');
    var file=evt.currentTarget.files[0];
    var reader = new FileReader();
    reader.onload = function (evt) {
        this.$scope.$apply(function($scope){
        this.$scope.myImage=evt.target.result;
      });
    };
    reader.readAsDataURL(file);
  };
我想把它转换成coffeescript语法。请帮帮我


谢谢

这是您的转换代码:

# CoffeeScript code converted from JavaScript
# from Georgi Naumov
# gonaumov@gmail.com for contacts and suggestions
@.$scope.callTestFuntion = ->
  @.blockingObject.render (dataURL) ->
    console.log 'via render'
    console.log dataURL.length
@.$scope.blockingObject.callback = (dataURL) ->
  console.log 'via function'
  console.log dataURL.length
  @.myCroppedImage = dataURL

handleFileSelect = (evt) ->
  console.log 'here'
  file = evt.currentTarget.files[0]
  reader = new FileReader()
  reader.onload = (evt) ->
    @.$scope.$apply ($scope) ->
      @.$scope.myImage = evt.target.result
  reader.readAsDataURL file
下面是编译后的结果:

// Generated by CoffeeScript 1.12.3
(function() {
  var handleFileSelect;

  this.$scope.callTestFuntion = function() {
    return this.blockingObject.render(function(dataURL) {
      console.log('via render');
      return console.log(dataURL.length);
    });
  };

  this.$scope.blockingObject.callback = function(dataURL) {
    console.log('via function');
    console.log(dataURL.length);
    return this.myCroppedImage = dataURL;
  };

  handleFileSelect = function(evt) {
    var file, reader;
    console.log('here');
    file = evt.currentTarget.files[0];
    reader = new FileReader();
    reader.onload = function(evt) {
      return this.$scope.$apply(function($scope) {
        return this.$scope.myImage = evt.target.result;
      });
    };
    return reader.readAsDataURL(file);
  };

}).call(this);

如果您只想转换代码,请使用


为什么要将JS转换为coffescript,最终/不可避免地会转换回JS?我刚刚修复了一个函数冗余返回的问题。祝你好运:)
@$scope.callTestFuntion = ->
  @blockingObject.render (dataURL) ->
    console.log 'via render'
    console.log dataURL.length
    return
  return

@$scope.blockingObject.callback = (dataURL) ->
  console.log 'via function'
  console.log dataURL.length
  @myCroppedImage = dataURL
  return

handleFileSelect = (evt) ->
  console.log 'here'
  file = evt.currentTarget.files[0]
  reader = new FileReader

  reader.onload = (evt) ->
    @$scope.$apply ($scope) ->
      @$scope.myImage = evt.target.result
      return
    return

  reader.readAsDataURL file
  return

# ---
# generated by js2coffee 2.2.0