Javascript: ReferenceError: MyClass is not defined

牧云@^-^@ 提交于 2019-12-13 14:13:26

问题


This is very basic. I try to instantiate a Class defined in an external .js file embedded. The code of the .js is simply this.

(function() {
  var MyClass;

  MyClass = (function() {

    function MyClass() {}

    MyClass.prototype.name = function(name) {};

    return MyClass;

  })();

}).call(this);

And the HTML is this

<!DOCTYPE html>
<html>

  <head>
    <title>Sample Page</title>
    <script src="file.js" type="text/javascript"></script>
  </head>

  <body>

  </body>
</html>

If I try on the console to instantiate the class but I see a ReferenceError: MyClass is not defined:

var myVar
myVar = new MyClass
> ReferenceError: MyClass is not defined

If I try to call MyClass directly from console I get the same error

> ReferenceError: MyClass is not defined

I'm sure I missing something terrible obvious here but yet I can figure it out what.

Updated: To create the javascript coded I'm using CoffeScript, the code is simply this.

class MyClass
  acc: (name) ->

The proposed answers codes when converted back to CoffeScript using http://js2coffee.org render into a different code and it still doesn't work. Wonder If there's a hint on CoffeScript to eject MyClass from local scope to the outer scope.


回答1:


My class is defined inside a closure. Rather what you want to do is "eject" it into the outer scope by setting it to the window object:

(function() {

    var myClass = ...

    window.myClass = myClass;

}).call( this );

Update: It seems you wanted it in CoffeeScript. Here you go:

(->

  myClass = (->
    myClass = ->
    myClass::name = (name) ->

    myClass
  )()

  window.myClass = myClass

).call this

JSBin Demo




回答2:


MyClass is defined into the local scope of the self-invoking function. You can try to initialize MyClass outside of the self-invoking function:

var MyClass;
(function() {

  MyClass = (function() {

    function MyClass() {}

    MyClass.prototype.name = function(name) {};

    return MyClass;

  })();

}).call(this);

Another thing you can do is:

(function() {
  this.MyClass = (function() {

    function MyClass() {}

    MyClass.prototype.name = function(name) {};

    return MyClass;

  })();

}).call(window);

In the code above you're simply invoking the anonymous function with the window context and setting value to the MyClass window's property (which makes MyClass global).

Another solution (which in my opinion is more unclear) is:

(function() {

  window.MyClass = (function() {

    function MyClass() {}

    MyClass.prototype.name = function(name) {};

    return MyClass;

  })();

}).call(this);

The only difference is that you explicitly say that the property MyClass of window will be equals to the result of the execution of the function.

Your CoffeeScript probably could be something like:

MyClass = undefined
(->
  MyClass = (->
    MyClass = ->
    MyClass::name = (name) ->

    MyClass
  )()
).call this


来源:https://stackoverflow.com/questions/13435029/javascript-referenceerror-myclass-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!