Working With Objects

Java script is an object oriented langugae, in other meaning we can say its an object based language.We must to work with objects when we learning or working with java script.In this topic we learn about working on objects by using strings, because everything in java script is an object,so it can be a string or a function or a datatype. Method of objects is very easy to learn because we also live in an object oriented world, whoever we see everthing is an object.

Objects:

There are two parts of objects.

Understand Objects with an Example:

For understanding deeply about objects we take an example here: Let take an example of a chair. If a chair is an object than it's color, legs, hands,rest-back and seating area all are properties.Now what about methods? This chair have weels so it can move, it can be round up and also can lean back its all the actions can do by chair are methods of chair object.It's very easy to understand, is't it?

chair object

Note: In java script all strings counting starts with position zero so we have to remember counting starts form position 0.

Here we learn that how can we use or manipulate the objects by using some methods.

lenght: This method is for knowing complete length value of any string.

indexOf methos: This method is use for know particulars position of string. indexOf method search particular string value from left to right.

Let see an example of object using strings, length and indexOf method.

var myString = "My name is ankit kalra",
length = myString.length,
position = myString.indexOf("a"),
position1 = myString.indexOf("a",position+1),
position2 = myString.indexOf("a",position1+1);
console.log(length); //22
console.log(position); //4
console.log(position1); //11
console.log(position2); //18

Let see an example of lastindexOf methos who is just opoosite of indexOf method. lastIndexOf method search particular string value from right to left. let see an example.

var myString = "My name is ankit kalra";
var position = myString.lastIndexOf("a"),
position1 = myString.lastIndexOf("a",position-1),
position2 = myString.lastIndexOf("a",position1-1),
position3 = myString.lastIndexOf("a",position2-1);
console.log(position); //21
console.log(position1); //18
console.log(position2); //11
console.log(position3); //4

Substr function: substr function is use for check position of sub-string in string. example--

var myString = "My name is ankit kalra";
var position = myString.substr(0,2); //Show positon starts with 0 and than show till 2 chars
console.log(position); //my
//we can do also this by using with both function indexOf and substr
var position = myString.indexOf("n"); //starts with n and show till index position no 4
var position = myString.substr(position,4); //name
console.log(position); //name

Replace function : replace function can replace modify the value of string by changing content.

var myString = "My name is ankit kalra";
var newString = myString.replace("ankit kalra","Kalra Ankit");
console.log(newString); //My name is kalra ankit.

Note: replace-function is a case-sensitive function.

Upper case or lower case function: toLowerCase and toUpperCase function(Modify values into Upper-case and Lower-case)

var myString = "My name is ankit kalra."
var lowercase = myString.toLowerCase();
console.log(lowercase); //my name is ankit kalra.

var uppercase = myString.toUpperCase();
console.log(uppercase); //MY NAME IS ANKIT KALRA.


Wconcert India © 2015   ·   All Rights Reserved

Free Web Hosting