diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..f673a71b7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5502 +} \ No newline at end of file diff --git a/assignments/prototypes.js b/assignments/prototypes.js index 5625c97cb..152d9f556 100644 --- a/assignments/prototypes.js +++ b/assignments/prototypes.js @@ -7,7 +7,7 @@ Each constructor function has unique properties and methods that are defined in their block comments below: */ - + /* === GameObject === * createdAt @@ -15,6 +15,15 @@ * dimensions (These represent the character's size in the video game) * destroy() // prototype method that returns: `${this.name} was removed from the game.` */ +function GameObject(attributes) { + this.createdAt = attributes.createdAt; + this.name = attributes.name; + this.dimensions = attributes.dimensions; +} + +GameObject.prototype.destroy = function () { + return `${this.name} was removed from the game.`; +}; /* === CharacterStats === @@ -22,6 +31,16 @@ * takeDamage() // prototype method -> returns the string ' took damage.' * should inherit destroy() from GameObject's prototype */ +function CharacterStats(attributes) { + GameObject.call(this, attributes) + this.healthPoints = attributes.healthPoints; +} + +CharacterStats.prototype = Object.create(GameObject.prototype); + +GameObject.prototype.takeDamage = function () { + return `${this.name} took damage.`; +}; /* === Humanoid (Having an appearance or character resembling that of a human.) === @@ -32,7 +51,20 @@ * should inherit destroy() from GameObject through CharacterStats * should inherit takeDamage() from CharacterStats */ - +function Humanoid(attributes) { + CharacterStats.call(this, attributes) + this.team = attributes.team; + this.weapons = attributes.weapons; + this.language = attributes.language; +} + +Humanoid.prototype = Object.create(CharacterStats.prototype); + +Humanoid.prototype.greet = function () { + return `${this.name} offers a greeting in ${this.language}.` +}; + + /* * Inheritance chain: GameObject -> CharacterStats -> Humanoid * Instances of Humanoid should have all of the same properties as CharacterStats and GameObject. diff --git a/assignments/this.js b/assignments/this.js index 969bbeeba..921e2e811 100644 --- a/assignments/this.js +++ b/assignments/this.js @@ -1,26 +1,64 @@ /* The for principles of "this"; * in your own words. explain the four principle for the "this" keyword below. * -* 1. -* 2. -* 3. -* 4. +* 1. Implicit Binding: points to the obj. on which the function is called +* 2. Explicit Binding: We can explicitly tell the JS engine to set 'this' to point a certain value using call, apply, or bind +* 3. Global: is the global obj. in a non strict mode. & undefined in a strict mode +* 4. new Binding: Using the new keyword constructs a new obj. & 'this' points it. * * write out a code example of each explanation above */ // Principle 1 - -// code example for Window Binding - +// code example for Window Binding (global) +console.log(this); +const allThis = function () { + console.log(this); + this.somethingg = "hiya"; +} +allThis(); // Principle 2 - // code example for Implicit Binding +const dog = { + name: 'sparky', + age: 3, + food: 'donuts', + activity: function (){ + return `${this.name} loves to eat ${this.food}, since he has just turned ${this.age}`; + } +} +console.log(dog.activity()); -// Principle 3 +// Principle 3 // code example for New Binding - +function Skateboards (color, model, year){ + this.color = color; + this.model = model; + this.year = year; +} +var skateboads1 = new Skateboards('electric blue', 'zig zagger', 2011); +var skateboads2 = new Skateboards('bright purple', 'baker', 2010); +var skateboads3 = new Skateboards('lovely lilac', 'death wish', 2014); +console.log(skateboads1); +console.log(skateboads2); +console.log(skateboads3); // Principle 4 +// code example for Explicit Binding + +const sponge ={ + name: "Spongebob" +} +const star ={ + name: "Patrick Star" +} +function whatsUp(passion1, passion2, passion3){ + return `eeee!! my name is ${this.name} and the activities that bring me joy are ${passion1}, ${passion2} and ${passion3}`; +} +console.log(whatsUp.call(sponge, 'jellyfish', 'the Krusty Krab', 'Krabby Paddies')); + +const starPassions = ['sleeping', 'eating', 'hanging with spongebob']; +console.log = (whatsUp.apply(star, starPassions)); -// code example for Explicit Binding \ No newline at end of file +const Spongebob = whatsUp.bind(sponge, 'jellyfish', 'the Krusty Krab', 'Krabby Paddies'); +// console.log(Spongebob()); \ No newline at end of file