Advance JS
Advance JS
A JavaScript object is an entity having state and behavior 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. We don’t create class to get the object. But, we direct create
objects.
Creating Objects in JavaScript:
There are 3 ways to create objects.
By object literal:
The syntax of creating object using object literal
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>
By creating instance of Object directly (using new keyword):
The syntax of creating object directly
var objectname=new Object();
New keyword is used to create object.
<script>
var emp=new Object();
emp.id=101;
emp.name=”Ravi Malik”;
emp.salary=50000;
document.write(emp.id+” “+emp.name+” “+emp.salary);
</script>
By using an object constructor (using this keyword):
you need to create function with arguments. Each argument value can be assigned in the current object by
using this keyword. The this keyword refers to the current object.
The example of creating object by object constructor,
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,"Vimal Jaiswal",30000);
document.write(e.id+" "+e.name+" "+e.salary);
</script>
JAVASCRIPT OWN OBJECTS
an object is a collection of properties, where each property is a key-value pair.
This example creates a new object called person:
const person = {
firstName: 'John',
lastName: 'Doe'
};
The person object has two properties: firstName and lastName.
a property of an object can be either own or inherited.
A property that is defined directly on an object is own while a property that the object receives from its
prototype is inherited.
The following creates an object called employee that inherits from the person object:
const employee = Object.create(person, {
job: {
value: 'JS Developer',
enumerable: true
}
});
The employee object has its own property job, and inherits firstName and lastName properties from its
prototype person.
The hasOwnProperty() method returns true if a property is own.
console.log(employee.hasOwnProperty('job')); // => true
console.log(employee.hasOwnProperty('firstName')); // => false
console.log(employee.hasOwnProperty('lastName')); // => false
console.log(employee.hasOwnProperty('ssn')); // => false
A property that is directly defined on an object is an own property. The obj.hasOwnProperty() method
determines whether or not a property is own.
THE DOM AND WEB
The document object represents the whole html document.
When html document is loaded in the browser, it becomes a document object. It is the root
element that represents the html document.
It has properties and methods. By the help of document object, we can add dynamic content to
our web page.
It is the object of window.
window.document Is same as document
“The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows
programs and scripts to dynamically access and update the content, structure, and style of a document."
Properties of document object:
if (password.length < 6) {
alert ("Password should be atleast 6 character long");
password.focus();
return false;
}