0

how to set and get parameter value?
I have function like this:

var File = function(){
  "use strict";
   var readFile = function(element){
     console.log(element);  //i want get paramater here
   }

   return {
     init:function(){readFile()}
   }
}();

and i implement this function like:

File.init('#image');

but i still get undefined

1
  • 2
    The init method has no parameter, and doesn't pass anything to readFile() Commented Jun 12, 2020 at 11:27

2 Answers 2

2

You almost got this. Try something like this instead:

var File = function(){
  "use strict";
   var readFile = function(element){
     console.log(element);  //i want get paramater here
   }

   return {
     init: function(element){ readFile(element) }
   }
}();

File.init('#element');

Sign up to request clarification or add additional context in comments.

Comments

1

You are defining your init as a function that takes no parameter. You should make init a function that takes an element as a parameter and uses it to call readFile.

var File = function(){
  "use strict";
   var readFile = function(element){
     console.log(element);  //i want get paramater here
   }

   return {
     init:function(element){
         readFile(element);
     }
   }
}();

Side note: a probably more organized way of writing your code is to use a class called File that has the required functionality:

class File {
  static readFile(element) {
    console.log(element);
  }
  //other functions you may need here....
}

File.readFile(document.querySelector('p'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.