0

enter code hereI created a new javascript file/object(File 2) and I'm trying to create the new object in file 1, however; i get an "object is not defined error". I have the reference present in the aspx file.

JS File 1

dosomething function()

{

    var RedNotesDisplay = new RedNotesDisplayController(redNotesArray, "22", draggedRowsContainer);
}

Js File 2

function RedNotesDisplayController(redNotesContent, JQueryPositionInformation, containerId)
{
    var _redNotesContent = redNotesContent;
    var _JQueryPositionFormation = JQueryPositionInformation;
    var _containerId = containerId;
    var _redNotesDiv = "";
}

Here's the error

var RedNotesDisplay = new RedNotesDisplayController(redNotesArray, "22", draggedRowsContainer);  <- 'RedNotesDisplayController' is undefined
jan86
  • 107
  • 2
  • 15
  • There's not really enough information to go on here. Could you try to clarify? What line is the error on, etc. – Karl Reid Jun 27 '17 at 17:17
  • `dosomething function()` is not valid javascript. Did you mean `function dosomething()` ? – James Jun 27 '17 at 17:18
  • What line is it saying the error is on? and where are redNotesArray, draggedRowsContainer defined – Patrick Evans Jun 27 '17 at 17:18
  • var RedNotesDisplay = new RedNotesDisplayController(redNotesArray, "22", draggedRowsContainer); is where the error lies – jan86 Jun 27 '17 at 17:24
  • 1
    The error means that `RedNotesDisplayController` doesn't exist where you are trying to access it. Since we don't know anything about how these two files are loaded there is nothing we can do to help you. – Felix Kling Jun 27 '17 at 17:28
  • are you including the js files in the file where the line with the error is thrown? – mjw Jun 27 '17 at 17:29
  • @mjw yes. I am referencing both files in the Aspx file I'm using. – jan86 Jun 27 '17 at 17:30
  • try coding var ns = { RedNotesDisplayController: function(){ // do work} }; –  Jun 27 '17 at 18:01

1 Answers1

0

Try this. What is going on here is we are creating a namespace called ns. Here is another article that helps articulate this concept and how it can be applied. How do I declare a namespace in JavaScript?

   var ns = {
    RedNotesDisplayController: function (redNotesContent, JQueryPositionInformation, containerId)
    {
        var _redNotesContent = redNotesContent;
        var _JQueryPositionFormation = JQueryPositionInformation;
        var _containerId = containerId;
        var _redNotesDiv = "";
        return "i am return string";
    }
};
var RedNotesDisplay = ns.RedNotesDisplayController([1,2,3], "22", "<div></div>");

// i use console.log to display the string in developer tools [Console]
console.log(RedNotesDisplay);

since ns could be undefined in another page, you are losing scope. Try this code to verify what your constructor is for this js.

<script type="text/javascript">
        var somethingNew = new ns.RedNotesDisplayController([1, 2, 3], "22", "<div></div>");
        alert(somethingNew.constructor);
    </script>

You should now get a response from localhost (my response for example) : function (n,t,i){var r=n,u=t,f=i;return"i am return string"}