Вы находитесь на странице: 1из 15

Shashank Sharma MHW Consulting Pvt. Ltd.

JavaScript (JS) is an interpreted computer programming language. It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed. However, it is used in mobile applications, desktop application widget and game developments as well. JavaScript is a prototype-based scripting language that is dynamic, is weakly typed. Its syntax was influenced by the language C. JavaScript copies many names and naming conventions from Java, but the two languages are otherwise unrelated and have very different semantics. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles.

JavaScript has strong object-oriented programming capabilities, even though some debates have taken place due to the differences in object-oriented JavaScript compared to other languages.

Object-oriented programming may be seen as the design of software using a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a collection of functions, or simply as a list of instructions to the computer. In OOP, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent little machine with a distinct role or responsibility.

Class - Defines the characteristics of the Object. Object - An Instance of a Class. Property - An Object characteristic, such as color. Method - An Object capability, such as walk.

Constructor - A method called at the moment of instantiation.


Inheritance - A Class can inherit characteristics from another Class. Encapsulation - A Class defines only the characteristics of the Object, a method defines only how the method executes.

Abstraction - The conjunction of complex inheritance, methods, properties of an Object must be able to simulate a reality model.
Polymorphism - Different Classes might define the same method or property.

Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is accomplished through a process of decorating existing objects which serve as prototypes. This model is also known as classless, prototype-oriented, or instance-based programming.

Core Objects
JavaScript has several objects included in its core; for

example, there are objects like Math, Object, Array, and String. The example below shows how to use the Math object to get a random number by using its random() method. Every object in JavaScript is an instance of the object Object and therefore inherits all its properties and methods.
For e.g.: alert(Math.random()); alert(new Date());

The Class
JavaScript is a prototype-based language which contains

no class statement, such as is found in C++ or Java. This is sometimes confusing for programmers accustomed to languages with a class statement. Instead, JavaScript uses functions as classes. Defining a class is as easy as defining a function. In the example below we define a new class called Person.
For e.g.: function Person() { }

The Object (Class Instance)


To create a new instance of an object obj we use the

statement new obj, assigning the result (which is of type obj) to a variable to access it later. In the example below we define a class named Person and we create two instances (person1 and person2).
For e.g.: function Person() { } var person1 = new Person(); var person2 = new Person();

The Constructor
The constructor is called at the moment of instantiation

(the moment when the object instance is created). The constructor is a method of the class. In JavaScript, the function serves as the constructor of the object; therefore, there is no need to explicitly define a constructor method. Every action declared in the class gets executed at the time of instantiation.
function Person() { alert(Person Instantiated); } var person1 = new Person(); var person2 = new Person();

The Property
Properties are variables contained in the class; every

instance of the object has those properties. Properties should be set in the prototype property of the class (function) so that inheritance works correctly.
function Person(gender) { this.gender = gender; alert('Person instantiated as ' + gender ); } var person1 = new Person('Male'); var person2 = new Person('Female'); //display the person1 gender alert('person1 is a ' + person1.gender); // person1 is a Male

The Method
Methods follow the same logic as

properties; the difference is that they are functions and they are defined as functions. Calling a method is similar to accessing a property, but you add () at the end of the method name, possibly with arguments. To define a method, assign a function to a named property of the class's prototype property; the name that the function is assigned to is the name that the method is called by on the object.
In the example shown we define

function Person(gender) { this.gender = gender; alert('Person instantiated'); } Person.prototype.sayHello = function() { alert(this.gender + ' says hello!!!'); };

var person1 = new Person('Male'); var person2 = new Person('Female');


// call the Person sayHello method. person1.sayHello(); // hello

and use the method sayHello() for the Person class.

Inheritance

// define the Person Class function Person() {}

Inheritance is a way to create a class as a specialized version of one or more classes (JavaScript only supports single class inheritance). The specialized class is commonly called the child, and the other class is commonly called the parent. In JavaScript you do this by assigning an instance of the parent class to the child class, and then specializing it.
In the example below, we define the class Student as a child class of Person. Then we redefine the sayHello() method and add the sayGoodBye() method.

Person.prototype.walk = function(){ alert ('I am walking!'); }; Person.prototype.sayHello = function(){ alert ('hello'); };


function Student() {// define the Student class Person.call(this); // Call the parent constructor }

Student.prototype = new Person(); // inherit Person


// correct the constructor pointer because it points to Person Student.prototype.constructor = Student; // replace the sayHello method Student.prototype.sayHello = function(){ alert('hi, I am a student'); } Student.prototype.sayGoodBye = function(){// add sayGoodBye method alert('goodBye'); } var student1 = new Student(); student1.sayHello(); student1.walk(); student1.sayGoodBye(); // check inheritance alert(student1 instanceof Person); // true alert(student1 instanceof Student); // true

Encapsulation

In the previous example, Student does not need to know how the Person class's walk() method is implemented, but still can use that method; the Student class doesn't need to explicitly define that method unless we want to change it. This is called encapsulation, by which every class inherits the methods of its parent and only needs to define things it wishes to change.

Abstraction

Abstraction is a mechanism that permits modeling the current part of the working problem. This can be achieved by inheritance (specialization), or composition. JavaScript achieves specialization by inheritance, and composition by letting instances of classes be the values of attributes of other objects. The JavaScript Function class inherits from the Object class (this demonstrates specialization of the model). and the Function.prototype property is an instance of Object (this demonstrates composition)

Polymorphism

Just like all methods and properties are defined inside the prototype property, different classes can define methods with the same name; methods are scoped to the class in which they're defined. This is only true when the two classes do not hold a parent-child relation (when one does not inherit from the other in a chain of inheritance).

var foo = function(){}; alert( 'foo is a Function: ' + (foo instanceof Function) ); alert( 'foo.prototype is an Object: ' + (foo.prototype instanceof Object) );

Вам также может понравиться