Objects in Javascript (1)
Objects in Javascript (1)
A JavaScript object is an entity having state and behavior (properties and method).For example: car, pen, bike, chair, glass, keyboard, monitor etc.
JavaScript is an object-based language. Everything is an object in JavaScript. JavaScript is template based not class based. Here, we don't create class to get the
object.
But, we direct create objects.
object={property1:value1,property2:value2.....propertyN:valueN}
<script>
emp={id:102,name:"Shyam Kumar",salary:40000}
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
<script>
var emp=new Object();
emp.id=101;
emp.name="Ravi Malik";
emp.salary=50000;
document.write(emp.id+" "+emp.name+" "+emp.salary);
</script>
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"Vimal Jaiswal",30000);
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
this.changeSalary=changeSalary;
function changeSalary(otherSalary){
this.salary=otherSalary;
}
}
e=new emp(103,"Sonoo Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
e.changeSalary(45000);
document.write("<br>"+e.id+" "+e.name+" "+e.salary);
</script>
1 Object.assign() This method is used to copy enumerable and own properties from a source object to a target object
2 Object.create() This method is used to create a new object with the specified prototype object and properties.
3 Object.defineProperty() This method is used to describe some behavioral attributes of the property.
5 Object.entries() This method returns an array with arrays of the key, value pairs.
7 Object.getOwnPropertyDescriptor() This method returns a property descriptor for the specified property of the specified object.
8 Object.getOwnPropertyDescriptors() This method returns all own property descriptors of a given object.
9 Object.getOwnPropertyNames() This method returns an array of all properties (enumerable or not) found.
10 Object.getOwnPropertySymbols() This method returns an array of all own symbol key properties.
12 Object.is() This method determines whether two values are the same value.
16 Object.keys() This method returns an array of a given object's own property names.
18 Object.seal() This method prevents new properties from being added and marks all existing properties as non-configurable.
19 Object.setPrototypeOf() This method sets the prototype of a specified object to another object.
1) By string literal
The string literal is created using double quotes. The syntax of creating string using string literal is given below:
<script>
var str="This is string literal";
document.write(str);
</script>
Output:
This is string literal
<script>
var stringname=new String("hello javascript string");
document.write(stringname);
</script>
Output:
hello javascript string
JavaScript String Methods:-
Let's see the list of JavaScript string methods with examples.
Methods Description
charCodeAt() It provides the Unicode value of a character present at the specified index.
indexOf() It provides the position of a char value present in the given string.
lastIndexOf() It provides the position of a char value present in the given string by searching a character from the last position.
search() It searches a specified regular expression in a given string and returns its position if a match occurs.
match() It searches a specified regular expression in a given string and returns that regular expression if a match occurs.
substr() It is used to fetch the part of the given string on the basis of the specified starting position and length.
substring() It is used to fetch the part of the given string on the basis of the specified index.
slice() It is used to fetch the part of the given string. It allows us to assign positive as well negative index.
toLocaleLowerCase() It converts the given string into lowercase letter on the basis of host current locale.
toLocaleUpperCase() It converts the given string into uppercase letter on the basis of host current locale.
split() It splits a string into substring array, then returns that newly created array.
trim() It trims the white space from the left and right side of the string.
<script>
var str="javascript";
document.write(str.charAt(2));
</script>
Output: v
<script>
var s1="javascript ";
var s2="concat example";
var s3=s1.concat(s2);
document.write(s3);
</script>
Output:
javascript concat example
JavaScript String indexOf(str) Method
The JavaScript String indexOf(str) method returns the index position of the given string.
<script>
var s1="javascript from javatpoint indexof";
var n=s1.indexOf("from");
document.write(n);
</script>
Output:
11
<script>
var s1="javascript from javatpoint indexof";
var n=s1.lastIndexOf("java");
document.write(n);
</script>
Output:
16
<script>
var s1="JavaScript toLowerCase Example";
var s2=s1.toLowerCase();
document.write(s2);
</script>
Output:
javascript tolowercase example
JavaScript String toUpperCase() Method
The JavaScript String toUpperCase() method returns the given string in uppercase letters.
<script>
var s1="JavaScript toUpperCase Example";
var s2=s1.toUpperCase();
document.write(s2);
</script>
Output:
JAVASCRIPT TOUPPERCASE EXAMPLE
<script>
var s1="abcdefgh";
var s2=s1.slice(2,5);
document.write(s2);
</script>
Output:
cde
JavaScript String trim() Method
The JavaScript String trim() method removes leading and trailing whitespaces from the string.
<script>
var s1=" javascript trim ";
var s2=s1.trim();
document.write(s2);
</script>
Output:
javascript trim
<script>
var str="This is JavaTpoint website";
document.write(str.split(" ")); //splits the given string.
</script>
JavaScript Date Object
The JavaScript date object can be used to get year, month and day. You can display a timer on the webpage by the help of JavaScript date object. You can use different Date
constructors to create date object. It provides methods to get and set day, month, year, hour, minute and seconds.
Constructor
You can use 4 variant of Date constructor to create date object.
o Date()
o Date(milliseconds)
o Date(dateString)
o Date(year, month, day, hours, minutes, seconds, milliseconds)
Output:
Current Date and Time: Fri Mar 19 2021 12:48:42 GMT+0530 (India Standard Time)
Let's see another code to print date/month/year.
<script>
var date=new Date();
var day=date.getDate();
var month=date.getMonth()+1;
var year=date.getFullYear();
document.write("<br>Date is: "+day+"/"+month+"/"+year);
</script>
Output:
Date is: 19/3/2021
Output:
Current Time: 12:48:42
JavaScript Boolean
JavaScript Boolean is an object that represents value in two states: true or false. You can create the JavaScript Boolean object by Boolean() constructor as given
below:
Boolean b=new Boolean(value);
<script>
document.write(10<20); //true
document.write(10<5); //false
</script>
Property Description
constructor returns the reference of Boolean function that created Boolean object.
Method Description
Window Object
The Window object is the top level object in the JavaScript hierarchy. The Window object represents a browser window. A Window object is created
automatically with every instance of a <body> or <frameset> tag.
IE: Internet Explorer, F: Firefox, O: Opera.
Collection Description
frames[] Returns all named frames in the window