Sei sulla pagina 1di 8

OBJECT ORIENTED JAVASCRIPT:

Class:
A class is set of Objects that share and inherit from same basic prototype.

Object:
Object contain multiple pieces of related data in one container.Multiple pieces of data , called
“properties”, are grouped within an object.

Properties can refer to numbers,strings,variables,arrays,functions, and even other objects .

There are three way to create object in javascript .

1. Using Object Literal or namespace


2. Using ‘new’ keyword
3. Using Constructor type
4. Object.create() [included in ECMAScript 5]

1. Using Object Literal:


2. Constructor type (using new keyword):
3. Object.create():

var app={ };

A function's prototype: A function's prototype is the object instance that will


become the prototype for all objects created using this function as a constructor.

An object's prototype: An object prototype is the object instance from which the
object is inherited.

To calculate length of object, we have to use for in because length property does
not support.

for(key in object)
{

you can use this(always refer to calling object) instead of object if you are using
for in inside method which is also associated with same object.
in built object property are as follows:

1. valueOf
2. constructor
3. toLocalString
4. toString
5. isPrototypeOf
6. propertyIsEnumerable
7. hasOwnProperty
All these property come form Object prototype.So you can use this property with
your own object.

Inheritance:
Passing down property is called inheritance.Inheritance helps avoid over coding
multiple properties and method into similar object.

Inheritance avoids duplicate memory storage.

All object that we creates directly inherited from Object Prototype.

Array Methods:

1. length
2. pop()
3. push()
4. shift()
5. reverse()
6. sort()
7. join()
8. reduce()
9. slice()
All these property and method come form Array prototype

String Methods:
1. length
2. charAt()
3. trim()
4. concat()
5. indexOf()
6. replace()
7. toUpperCase()
8. toLowerCase()
9. substring()

You can create your own method, which will be accessible to all string object.

In above example, we have created countAll() method which is accessible to all


string object.

Number Methods:
1. toFixed()
2. toExponential()
3. toPrecision()

Function Methods:
1. name
2. call()
3. bind()
4. apply()

For example:

function getName()
{
return “yes, you are right”
}
getName.name;
getName.call();
getName.bind();
getName.apply();

Prototype:

A prototype is like a blueprint object for the object we are trying to create.
Question: Is JavaScript case sensitive?
Yes, JavaScript is a case sensitive..

Question:What are different Data-Types of JavaScript?


Following are different data-type in JavaScript.
1. String
2. Number
3. Boolean
4. Function
5. Object
6. Null
7. Undefined

Question: What is an Object?


The object is a collection of properties & each property associated with
the name-value pairs.
The object can contain any data types (numbers, string, arrays, object
etc.).

Question: What are different two ways of creating an object?


Object Literals: This is the most common way to create the object with
object literal.
For Example:
var emptyObj= {};

Object Constructor: It is way to create object using object constructor


and the constructor is used to initialize new object.
For Example:
Var obj = new Object();

Question: What is scope variable in JavaScript?


The scope is set of objects which can be variables and function.
"Scope variable" can be have global scope variable and local scope
variable.

Question: Give an example creating Global variable


Global variable: A variable which can be variable from any where of the
page.
Following are different two ways.
First Way Declare the JavaScript variable at the top of JavaScript code
and out of function & objects.
var globalVariable1 ='This is global variable 1'

Second WayDeclare a varaible without "var" in Function.


function testfunction(){
globalVariable2 ='This is global variable 2'
}

Question: Give an example creating Global variable


Local variable: A variable which can be local and can't access globally.
When we declare a local varriable, Its local and can't access globally. It
must create using "var" keyword.
function testfunction1(){
var localVariable ='This is local variable '
}

Question: What is public, private and static variables in


JavaScript?
Public Varaible: A variable which associate to object and is publicily
available with object.
For Example:
function funcName1 (name) {
this.publicVar='1';
}

Private Variable: A variable which associate to object and is limited


available.
For Example:
function funcName2 (name) {
var privateVar='1';
}

Static variable: A static member is shared by all instances of the class


as well as the class itself and only stored in one place.
For Example:
function funcName3 (name) {

}
// Static property
funcName3.name = "Web Technology Experts Notes";
Question: How to achieve inheritance in JavaScript
"Pseudo classical inheritance" and "Prototype inheritance"

Question: What is closure in JavaScript?


When we create the JavaScript function within another function and the
inner function freely access all the variable of outer function.

Question: What is prototype in JavaScript?


All the JavaScript objects has an object and its property called prototype
& it is used to add and the custom functions and property. See Following
example in which we create a property and function.
var empInstance = new employee();
empInstance.deportment = "Information Technology";
empInstance.listemployee = function(){

Potrebbero piacerti anche