Created
January 28, 2019 10:36
-
-
Save fleepgeek/c8b9451bb5e45410347451ea487ccec3 to your computer and use it in GitHub Desktop.
A simple gist to show how to use JavaScript objects and arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Objects are containers for named values called properties or methods. | |
Objects are mutable ie its values can be changed other js variables are immutable | |
Objects are passed/called by reference not value | |
eg var obj = {name: "John"}; | |
var x = obj; | |
x.name = "Mike"; This would also change the value of obj.name to "Mike" | |
Primitive data types (number, boolean, string) are passed by value | |
Date, Math, Array, RegExp, etc are all objects | |
NB: Math does not inherit from Object.prototype because it is a global object | |
Almost everything in JS is an object(even functions) | |
*/ | |
// var student = { | |
// name: "John" | |
// } | |
// console.log(student); | |
// delete student.name; | |
// student.age = 22; | |
// console.log(student); | |
// var x = new String("Hi"); | |
// // x = "Hi"; | |
// console.log(x); | |
// function Woman(firstName, lastName) { | |
// this.firstName = firstName; | |
// this.lastName = lastName; | |
// // this.fullName = function() { | |
// // return firstName + " " + lastName; | |
// // } | |
// } | |
// Woman.prototype.fullName = function() { | |
// return this.firstName + " " + this.lastName; | |
// }; | |
// var woman1 = new Woman("Shade", "Adebayo"); | |
// console.log(woman1.fullName()); | |
// console.log("firstName" in woman1) | |
// for(var prop in woman1){ | |
// console.log(prop); | |
// } | |
// for(var prop in woman1){ | |
// if(woman1.hasOwnProperty(prop)) { | |
// // hasOwnProperty() checks only props declared | |
// // explicitly in the constructor ie excludes | |
// // inherited from the prototype | |
// console.log(prop); | |
// } | |
// } | |
// var x = Math.PI; // Returns PI | |
// var y = Math.sqrt(16); // Returns square root | |
// console.log(x, y); | |
// var nums = [2, 4, 6, 8, 10]; | |
// var total = 0; | |
// for(var i = 0; i < nums.length; i++) { | |
// // if(nums[i] === 6) continue; | |
// // console.log(nums[i]); | |
// total += nums[i]; | |
// } | |
// console.log(total); | |
// console.log(nums[2]); | |
// var ages = new Array(3); | |
// ages[0] = 23; | |
// ages[1] = 19; | |
// ages[2] = 33; | |
// ages[3] = 25; | |
// ages[4] = 17; | |
// console.log(ages[0]) | |
// ages.push(12); | |
// ages.pop(); | |
// ages.shift(); | |
// ages.unshift(10, 24); | |
// splice() is used to remove and add elements | |
// var filteredAges = ages.slice(1, 3); | |
// console.log(filteredAges); | |
// console.log(ages); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment