0

I made an object inside function. I want to call object methods in one statement.

(function(){
        s4 = function(getSection){
            var self = {
            name: function(getname){
                console.log("Name of the Student " +getname);
            },

            subject: function(getsub){
                console.log(" & Studying " +getsub);
            }

        }
     return self;   
        }
    })();
 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Student Details</title>
</head>
<body>
  <div class="hello">
      <h1>Student Details</h1>
  </div>
    <script src="script.js"></script>
    <script>
        s4("5th").name("John"); //This works fine
        s4("5th").subject("Python"); //This works fine
        s4("5th").name("John").subject("Python"); //Shows error: SCRIPT5007: Unable to get property 'subject' of undefined or null reference


    </script>
</body>
</html>

When I call s4("5th").name("John").subject("Python"); Shows error:

SCRIPT5007: Unable to get property 'subject' of undefined or null reference

Please help me solve this issue. Thanks in advance.

2
  • 2
    Because name function is not returning anything Commented Oct 13, 2016 at 5:33
  • @jeffcarey is correct. Name function is not returning any object, that's why you are getting null or undefined reference. Commented Oct 13, 2016 at 5:35

4 Answers 4

2

In your methods, return the object. return this in name and subject. This is called method chaining and it works by returning an object.

(function() {
  s4 = function(getSection) {
    var self = {
      name: function(getname) {
        console.log("Name of the Student " + getname);
        return this;
      },
      subject: function(getsub) {
        console.log(" & Studying " + getsub);
        return this;
      }
    }
    return self;
  }
})();

s4("5th").name("John");
s4("5th").subject("Python");
s4("5th").name("John").subject("Python");

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

Comments

2

That is called method chaining and is normally achieved by making each function that doesn't return some other value, return this;:

name: function(getname){
    console.log("Name of the Student " +getname);
    return this;
},

subject: function(getsub){
    console.log(" & Studying " +getsub);
     return this;
}

Then s4("5th").name("John").subject("Python"); will work since name("John") returns the result of s4("5th").

1 Comment

Can we concatenate both methods while calling method in a chain
0

Your code should be

(function() {
  s4 = function(getSection) {
    var self = {
      name: function(getname) {
        console.log("Name of the Student " + getname);
        return this;
      },
      subject: function(getsub) {
        console.log(" & Studying " + getsub);
        return this;
      }
    }
    return self;
  }
})();
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Student Details</title>
</head>

<body>
  <div class="hello">
    <h1>Student Details</h1>
  </div>
  <script src="script.js"></script>
  <script>
    s4("5th").name("John"); //This works fine
    s4("5th").subject("Python"); //This works fine
    s4("5th").name("John").subject("Python"); //Shows error: SCRIPT5007: Unable to get property 'subject' of undefined or null reference
  </script>
</body>

</html>

And now you will not get error for calling function subject()

Comments

-1

Your function

name: function(getname){
        console.log("Name of the Student " +getname);
    }

does not return this. Change it to:

name: function(getname){
        console.log("Name of the Student " +getname);
        return this;
    }

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.