1

What's wrong with below code? Why I got an Uncaught SyntaxError: Unexpected token (

function Person(name){
    sayHi: function(){
      return "hi " + this.name;
    }
}

var john = new Person("John");
alert(john.sayhi());
4
  • 1
    The first '{' starts a function, not an object literal.. Commented Jul 3, 2015 at 4:52
  • 3
    function Person(name){ return {sayHi: function(){ return "hi " + this.name; }} } Commented Jul 3, 2015 at 4:53
  • @user2864740 how to make it into an object? Commented Jul 3, 2015 at 4:55
  • 1
    Besides what @DanielTate said, you are calling 'sayhi' but you defined it as 'sayHi' Commented Jul 3, 2015 at 4:55

6 Answers 6

2

The problem is that declaration of sayHi is invalid because you are not in the same context as an object literal, so you are having a syntax error.

var o = { sayHi: function() {}} ;

you could do it using "this" (Because you are using "new" already) :

function Person(name){

    this.sayHi = function(){
        return "hi " + name;
    }
}

var john = new Person("John");
alert(john.sayHi());

another way is

function Person(name){

   return {
      sayHi : function() {
         return "hi " + name;
      }
   }
}    

var john = Person("John");
alert(john.sayHi());
Sign up to request clarification or add additional context in comments.

Comments

1

You should use this to make the variables and functions public of a class. If you dont use this keyword with variables then they can only accessible inside the class.

Please check this code.

function Person(name){
  this.sayHi = function(){
    return "hi " + name;
  }
}
var john = new Person("John");
alert(john.sayHi());

Comments

0

You can change like this:

function Person(name){
    this.sayHi = function(){
      return "hi " + name;
    }
}

var john = new Person("John");
alert(john.sayHi());

Or something like this

function Person(name){
    this.name = name;
}
Person.prototype.sayHi = function(){
      return "hi " + this.name;
}
var john = new Person("John");
alert(john.sayHi());

Comments

0

Using an Object Constructor:

function Person(name){
    this._name = name;
    this.sayHi = function(){
      return "hi " + this._name;
    }
}

var john = new Person("John");
alert(john.sayHi());

Comments

0

Hi Please check this code

(function( $ ){
   $.fn.sayHi = function(name) {

      return name;
   }; 
})( jQuery );

alert($('body').sayHi('dasd'));

Comments

0

For additional clarity you can take the sayhi method out of the Person Object declaration as below.

Declare Person Object, including name and sayhi function.

function Person(nameString){
        this.name = nameString;
        this.message = sayhi;
    }

State what sayhi should do.

function sayhi(){
    return hiString = "hi " + this.name;
}

Initiate your Person

var john = new Person("John");

Call sayhi function on your Person Object and alert the return value

alert(john.sayhi());

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.