0
(function (window) {
    var speakWord = "Hello";
    var HelloSpeaker = {
        function speak(name) {
        console.log(speakWord + " " + name)}
    }
    window.HelloSpeaker = HelloSpeaker
})(window)

I dont know why line "function speak(name) {" returns "Uncaught SyntaxError: Unexpected identifier error"?

1
  • 1
    You are defining a simple object (HelloSpeaker) that contains a full function definition. If you are trying to assign a function to a variable, that's not how it's done. see this Commented Apr 7, 2022 at 17:17

1 Answer 1

4

You're assigning a method to the object, you should be doing that like this

{ property: value }, in your case the "value" would be a function

(function(window) {
  var speakWord = "Hello";
  var HelloSpeaker = {
    speak: function(name) {
      console.log(speakWord + " " + name)
    }
  }
  window.HelloSpeaker = HelloSpeaker;
})(window)

window.HelloSpeaker.speak('ananth');

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

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.