calling prototyped function in javascript without creating an object

javascript prototype
javascript call function by name
javascript prototype inheritance
javascript prototype explained
how to call a function inside a function in javascript
javascript object
javascript extend object
javascript instantiate class

I am trying to call a prototyped function from another javascript file but it doesn't seem to work.

BuddyList.js :

function BuddyList(){ .. }

BuddyList.prototype = {
    addBuddy : function(buddyName){...}
}

UserOptions.js:

function UserOptions(){
....
BuddyList.addBuddy(username);
}

I get an error stating that BuddyList does not have a method called addBuddy

You don't need to use the .prototype method. Since everything in JS is an object, you can simply extend BuddyList with a new function:

function BuddyList(){
     // do whatever
}

BuddyList.addBuddy = function(username){
      // do whatever
}

It is said that all Javascript objects have a prototype property, but I , Let's create an object o from function f with its own properties a and JavaScript does not have "methods" in the form that class-based languages define them. calling o.m in this case, 'this' refers to o var p = Object.create(o);  Well, to be exact, the properties and methods are defined on the prototype property on the Objects' constructor functions, not the object instances themselves. In JavaScript, a link is made between the object instance and its prototype (its __proto__ property, which is derived from the prototype property on

.addBuddy is defined on objects that are constructed from the BuddyList constructor.

You can not call a method of an object like you are trying to do.

You might want to create it as a "static" function

BuddyList.addBuddy = function(buddyName) { ... }

You can then call it from the prototype aswell

BuddyList.prototype.addBuddy = function(buddyName) {
    BuddyList.addBuddy.apply(this, arguments)
}

You would need to check for any references to a BuddyList object inside the addBuddy function though.

Remember BuddyList is an object constructor. Calling the addBuddy method on the constructor doesn't make sense. I think you actaully want to do this

buddyList = new BuddyList;
buddyList.append(someBuddy);

In theory if there's only ever one BuddyList then you may want to look for

BuddyList = new BuddyList;

That will overwrite your constructor with an object inherited from the constructor.

You can always call the addBuddy method directly without an object through the prototype as follows:

BuddyList.prototype.addBudy.call(this, someBuddy)

It really depends on what your trying to do.

Prototype in JavaScript, This call makes a truly exact copy of obj , including all properties: It gave the ability to create objects with a given prototype, but did not Why was __proto__ replaced by the functions getPrototypeOf/setPrototypeOf ? If you have a reference to an instance of the class (that someone else created for you) then just call the method. If, on the other hand, you haven't it then you have to make the method static (if possible).

You can't access a prototype function until and unless you create an instance for the object or function. So instead of

BuddyList.addBuddy(buddyName) 

you have to write

new BuddyList().addBuddy(buddyName)

Can we invoke a constructor without creating its object, After that you can create new object and use it by: var rendererInstance = new You are not assigning a function with Renderer.prototype. All JavaScript objects inherit properties and methods from a prototype. We also learned that you can not add a new property to an existing object constructor: To add a new property to a constructor, you must add it to the constructor function: The Object.prototype is on the top of the prototype inheritance chain:

What is the difference between call and apply?, You can't get very far in JavaScript without dealing with objects. The most common way to create an object is with curly braces {} and you add and we'll call the function itself a “constructor function” since it's responsible for  In a function definition, this refers to the "owner" of the function. In the example above, this is the person object that "owns" the fullName function. In other words, this.firstName means the firstName property of this object. Read more about the this keyword at JS this Keyword. JavaScript methods are actions that can be performed on objects.

Inheritance and the prototype chain, Functions in JavaScript are special objects which can be “called”. you make, but not mandatory for internal functions provided by JavaScript (and function returned by bind ). (see below) of the a newly created object from that function (​using new keyword). hasOwnProperty and Function.prototype.call . The call() allows for a function/method belonging to one object to be assigned and called for a different object. call() provides a new value of this to the function/method. With call() , you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.

Prototype methods, objects without __proto__, Most of the time, JavaScript's prototype confuses people who have just started to learn If you have ever worked with JavaScript arrays or objects or strings, you have called arr2 then arr2.test() will throw an error “TypeError: arr2.test is not a function”. Let's create our own simple constructor function: The value of this, when used in an object, is the object itself. In a constructor function this does not have a value. It is a substitute for the new object. The value of this will become the new object when a new object is created.