Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP javascript and Simple Class Instantiation

Tags:

javascript

oop

First of all I'm sorry if this is a stupid question. I've written two code snippets bellow. The first code snippet found from here written by John Resig and undoubtedly he's one of the bests and the second code snippet is modified by me from the original code only to understand the difference but I'm not sure actually what is the difference between both and what I can and can't do with both comparatively. Please someone help me to understand the difference. Thanks.

    function makeClass(){
        return function(args){
            if ( this instanceof arguments.callee ) {
                if ( typeof this.init == "function" )
                    this.init.apply( this, args.callee ? args : arguments );
                }
                else  return new arguments.callee( arguments );
        };
    }
    var User = makeClass();
    User.prototype.init = function(first, last){
      this.name = first + " " + last;
    };
    var user = User("John", "Resig");
    console.log(user);

Modified version

    function myClass(args)
    {
        if (this instanceof arguments.callee) 
        {
            this.init = function(first, last){
                this.name = first + " " + last;
            };
            this.init.apply( this, args.callee ? args : arguments );
        }
        else    return new arguments.callee( arguments );
    }
    var obj = new myClass('Sheikh', 'Heera');
            console.log(obj);

Why should I use the object's prototype to add a method (after making an instance) instead of writing it inside the constructor ?

like image 259
The Alpha Avatar asked Apr 14 '12 19:04

The Alpha


People also ask

How do you instantiate a class in JavaScript?

The new operator instantiates the class in JavaScript: instance = new Class() . const myUser = new User(); new User() creates an instance of the User class.

Can we use OOP concept in JavaScript?

JavaScript is not a class-based object-oriented language. But it still has ways of using object oriented programming (OOP). In this tutorial, I'll explain OOP and show you how to use it. The most popular model of OOP is class-based.

What are OOPS concepts in JavaScript?

The characteristics of an Object are called Properties in Object-Oriented Programming and the actions are called methods. An Object is an instance of a class. Objects are everywhere in JavaScript, almost every element is an Object whether it is a function, array, or string.

What is an instantiation OOP?

In the OOP language C#, instantiation describes the processes of creating a new object for a class using a new keyword.


1 Answers

A primary reason to define a method on the object's prototype rather than in the constructor is that the method defined in the prototype is immediately available to and shared by (in memory) all instances of the object.

By defining the method in the constructor, that method is specific to the instance of the object creating it. If you insantiate 10 of your object, you'll have 10 copies of the method in memory, even though they are all the same, until you modify one of them.

To be clear though, by shared among object instances, I do not mean to say that the method operates statically on any properties it accesses. Those properties (if defined with this.) are still instance-specific. It's just that you don't end up defining multiple copies of the same method.

like image 142
Michael Berkowski Avatar answered Oct 12 '22 16:10

Michael Berkowski