0

I am using objects in JavaScript and try to call a function in a object with a parameter like:

obj.func(parameter);

The function is yet simple and just log the parameter in the console, or should do it.

func: function (txt) {
    console.log(txt);
}

The console just returns

undefined

which means, as far i know, there is no parameter given.

Can anyone help me?

7
  • 4
    That would mean that the value of parameter is undefined. Commented Jun 14, 2016 at 15:07
  • 1
    Where is parameter set? If you never set the value, you're passing undefined into the function. Commented Jun 14, 2016 at 15:07
  • 1
    We need more information. Where is parameter coming from how is it set? Commented Jun 14, 2016 at 15:08
  • 1
    Please post the section where you call the function. You're either passing undefined or not passing any parameters Commented Jun 14, 2016 at 15:12
  • 1
    Works for me. "which means, as far i know, there is no parameter given" Not necessarily, it can also mean it was given, and has the value undefined. Commented Jun 14, 2016 at 15:13

2 Answers 2

0

It will be actually a Reference Error if you have not declared the parameter

Since it is undefined most likely it is uninitialized and left only with var parameter;

Initialized with some value like

var parameter ="abc";

In js Default Value of a variable is undefined

Check this JSFIDDLE

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

Comments

0

From what I understand you either have not defined your object correctly or the parameter is undefined. Check the following code:

// Say you defined obj as follows:
var obj = {
  name: 'some name',
  data: 'some data',
  func: function (txt) {
    console.log(txt);
  }
}
// Say you define paramater as follows:
var parameter = 'I am a parameter!';
// And finally you call the function like so:
obj.func(parameter);
// Or maybe like so:
obj.func('I am another parameter!');

You have to instantiate your complex object properly and define a value for your parameter to show up in the console before calling your function. By default the parameter's value is undefined, thus it shows that in the console.

5 Comments

@Deep I would ask the same. Given what the OP asked, this is the kind of solution that one can suggest for the problem...
Yes,i believe it should be mandatory to give some explanation for down voting an answer. it will help the person to understand at least what wrong he did, if he did any.
I hope the downvoter can explain themselves so I can see if I have made any mistake or my answer was inappropriate.
I asked myself the same, but dont worry i havent done this. I cant even vote :)
@prxy1 I can tell from your reputation that you are a new member. Anyways if you found any of the answers useful, upvote and/or approve them when you get the priveleges! Have a nice day! :)

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.