Skip to content

Instantly share code, notes, and snippets.

@arnonate
Created September 18, 2020 19:27
Show Gist options
  • Save arnonate/1512684132654cdd7ea6a5e0404d3944 to your computer and use it in GitHub Desktop.
Save arnonate/1512684132654cdd7ea6a5e0404d3944 to your computer and use it in GitHub Desktop.
document.getElementById("app").innerHTML = `
<h1>Hello Vanilla!</h1>
<div>
We use the same configuration as Parcel to bundle this sandbox, you can find more
info about Parcel
<a href="https://parceljs.org" target="_blank" rel="noopener noreferrer">here</a>.
</div>
`;
var marks = { physics: 98, maths: 95, chemistry: 91 };
// Create a new Object
var marks2 = {};
var marks3 = new Object();
// Parsing and stringifying
// returns "{"physics":98,"maths":95,"chemistry":91}"
JSON.stringify(marks); // Get object from string
JSON.parse('{"physics":98,"maths":95,"chemistry":91}');
// Iterating over an object
var highScore = 0;
for (i of Object.keys(marks)) {
if (marks[i] > highScore) highScore = marks[i];
}
// Return a list of values in Object
Object.values(obj);
// Accessing protype methods
marks.hasOwnProperty("physics"); // returns true
marks.hasOwnProperty("greek"); // returns false
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var newCar = new Car("Honda", "City", 2007);
console.log(newCar instanceof Car); // returns true
// .freeze allows us to make sure properties can't be modified
var marks = { physics: 98, maths: 95, chemistry: 91 };
finalizedMarks = Object.freeze(marks);
finalizedMarks["physics"] = 86; // throws error in strict mode
console.log(marks); // {physics: 98, maths: 95, chemistry: 91}
// .seal allows us to make configurable properties
// but ensure no new properties are added
var marks = { physics: 98, maths: 95, chemistry: 91 };
Object.seal(marks);
delete marks.chemistry; // returns false as operation failed
marks.physics = 95; // Works!
marks.greek = 86; // Will not add a new property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment