Example of creating a constructor function.
var epicFailVideos = function(epicRating, hasAnimals) {
this.epicRating = epicRating;
this.hasAnimals = hasAnimals;
}
var parkourFail = new epicFailVideos(7, false);
var corgiFall = new epicFailVideos(4, true)
object oriented programing
Key notes
use
<tabel>
element in HTML.
The new keyword and the object constructor create a blank object. You can then add properties and methods to the object.
var hotel = new Object();
hotel.name= 'quay';
hotel.rooms = 40;
hotel.booked = 25;
hotel.checkAvailability = function() {
return this.rooms - this.booked;
};
To update values of properties, use dot notation or square brackets. They work on objects created. To delete properties use
delete
keyword;
hotel.name = 'park'; //changes property
delete hotel.name; // deletes property
you can use
functions
to createobjects
.
function hotel(name,rooms, booked){
this.name = name; // properties
this.rooms = rooms;
this.booked = booked;
this.checkAvailability = function() { //method
return this.rooms - this.booked;
};
}
You can use the
new
keyword to followed by a function to created a new object
var quayHotel = new hotel("quay', 40, 25);
Type | Description | Example |
---|---|---|
Browser Object Model | Contains objects that represent the current browser window or tab. Like browser history | |
Document Object Model | Uses objects to create a representation of the current page. It creates a new object for each link. | |
Global Javascript Objects | represent things that the java script language needs to create a model of. |
Tell you about the current page
Property | Description | Example |
---|---|---|
document.title | Title of current document | |
document.lastModified | Date on which document was last modified | |
document.URL | Returns string containing URL of current document | |
document.domain | returns domain of current document |
Amending contents of the page Method | Description| Example —- | —- | —- document.write() | writes text to document document.getElementById() | Returns element, f there is an element with the value of the id attribute that matches document.querySelectionAll() | Returns list of elements that match a CSS selector, which is specified as a parameter document.createElement() | Creates new element (p222) document.createTextNode() | Creates new text node (p222)
Property | Description | Example |
---|---|---|
length | returns number of character in the string in most cases |
Method | Description | Example |
---|---|---|
toUpperCase() | Changes string to uppercase characters | |
toLowerCase() | Changes string to lower case characters | |
charAt() | Takes an index number as a parameter and returns the character found at that position | |
indexOf() | Returns index number of the first time a character or set of characters ifs found within the string | |
lastIndexOf() | Returns index number of the last time a character or set of characters is found within a string. | |
substring() | Returns character found between two index numbers where the character for the first index number is included and the character for the last index number is not included. | |
split() | When a character is specified, it splits the string each time it is found, then stores each individual part in an array. | |
trim() | removes whitespace from start and end of string | |
replace() | Like find and replace, it takes one value that should be found, and another to replace it. (only replaces the first match it finds) |
Number Objects
Method | Description | Example |
---|---|---|
isNAaN() | Checks if the value is not a number | |
toFixed() | Rounds to specified number of decimal places | |
toPrecision() | Rounds to total number of places | |
toExponential() | Returns a string representing the number in exponential notation. |
Math Objects
Property | Description | Example |
---|---|---|
Math.PI | Returns pi (3.14) |
Method | Description | Example |
---|---|---|
Math.round() | Rounds number to the narest integer | |
Math.sqrt(n) | Returns square root of positive nubmer | |
Math.ceil() | Rounds number uup to the nearest integer | |
Math.floor() | Rounds number down to the nearest integer | |
Math.random() | Generates a random number between 0 |
Date Objects