Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:16:42+00:00 2026-06-09T08:16:42+00:00

I am writing javascript code with revealing prototype pattern for the first time. I

  • 0

I am writing javascript code with revealing prototype pattern for the first time. I am having problems. When I call add function when user clicks add button then it shows me this error in the console.

Uncaught TypeError: Cannot call method 'add' of undefined

How can I solve this problem?

here is my script.js code

$(function () {
var todo = Todo('contents');

$('.addBtn').on('click', function() {
    var name = $(this).parent().find('input[type="text"]').val();
    todo.add(name);
});

$('.contents').on('click', '.remove', function() {
    var el = $(this).parent();
    todo.remove(el);
});

$('.contents').on('click', '.update', function() {
    var dom = $(this);
    todo.addUpdateField(dom);
});

$('.contents').on('click', '.updateBtn', function() {
    var el = $(this);
    todo.update(el);
});

});

here is my todo.js code

var Todo = function(c) {
    this.contents = $('.' + c);
};

Todo.prototype = function() {
var showel = function (d) {
    this.contents.prepend(d);
},

add = function (name) {
    if(name != "") {
        var div = $('<div class="names"></div>')
        .append('<span>' + name + '</span>')
        .append("<button class='update' class='update'>Edit</button>")
        .append("<button class='remove' name='remove'>Remove</button>");
    }

    return showel(div);
},

addUpdateField = function (dom) {
    var name = dom.parent().find('span').text(),
        field = $('<input type="text" value="' + name + '" />'),
        update = $('<button class="updateBtn">Update</button>');

    dom.parent().html('').append(field).append(update);

    return;
},

update = function(el) {
    var val = el.parent().find('input').val();
    el.parent().html('<span>' + val + '</span>')
    .append('<button class="update" class="update">Edit</button>')
    .append('<button class="remove" class="remove">Remove</button>');

    return;
},

remove = function (el) {
    return el.remove();
};

return {
    add             : add,
    update          : update,
    remove          : remove,
    addUpdateField  : addUpdateField
};

}();

Update

After changing
var todo = Todo('contents');

to

var todo = new Todo('contents');

I get this error

Object [object Object] has no method 'add'

update 2

here is my on jsfiddle

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:16:43+00:00Added an answer on June 9, 2026 at 8:16 am

    You’re trying to assign Todo‘s prototype to a self-calling function. However, the prototype is just getting assigned to a function and not the expected return object.

    Here’s your working fiddle. I assigned the prototype directly to an object with your methods.

    Also, JS parses from top to bottom. Therefore, define your new instance of Todo after you declare what Todo is.

    This is what a self-calling function should look like:

    Todo.prototype = (function() {
    
        // ...
    
        return {
            add: add,
            update: update,
            remove: remove,
            addUpdateField: addUpdateField
        };
    
    })();​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing javascript code for a web emulator containing function keys. The user is
I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed
I'm writing a piece of client-side javascript code that takes a function and finds
From time to time, I find myself writing server code that produces JavaScript code
I'm writing some MVC JavaScript code using Prototype. The problem is that when I
i am writing javascript code on nodepad++,i am at beginner level and write just
I'm writing some Javascript code that generates an XML document in the client (via
I'm writing some Javascript code and I'm trying to change the current page as
I'm writing a piece of code in JavaScript that is supposed to replace all
I'm writing a simple Javascript library that makes use of some WebGL code. I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.