// add properties to this object!
job : "programmer",
married : false
};
function Person(job, married) {
this.job = job;
this.married = married;
}
// create a "gabby" object using the Person constructor!
var gabby = new Person("student",true);
======================================================
function Person(job, married) {
this.job = job;
this.married = married;
// add a "speak" method to Person!
this.speak = function() {
console.log("Hello!");
};
}
var user = new Person("Codecademy Student",false);
user.speak();
=====================================================
function Person(name,age) {
this.name = name;
this.age = age;
}
// Let's make bob again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 35);
function Circle(radius) {
this.radius = radius;
}
====================================================
function Cat(name, breed) {
this.name = name;
this.breed = breed;
}
// let's make some cats!
var cheshire = new Cat("Cheshire Cat", "British Shorthair");
var gary = new Cat("Gary", "Domestic Shorthair");
// add a method "meow" to the Cat class that will allow
// all cats to print "Meow!" to the console
Cat.prototype.meow = function() {
console.log("Meow!");
}
// add code here to make the cats meow!
cheshire.meow();
gary.meow();
====================================================
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
this.isAlive = true;
}
function Penguin(name) {
this.name = name;
this.numLegs = 2;
}
function Emperor(name) {
this.name = name;
this.saying = "Waddle waddle";
}
// set up the prototype chain
Penguin.prototype = new Animal();
Emperor.prototype = new Penguin();
var myEmperor = new Emperor("Jules");
console.log(myEmperor.saying); // should print "Waddle waddle"
console.log(myEmperor.numLegs); // should print 2
console.log(myEmperor.isAlive); // should print true
=====================================================
function Person(first,last,age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
var john = new Person('John','Smith',30);
var myFirst = john.firstName;
var myLast = john.lastName;
//declare variable myAge set to the age of the john object.
var myAge = john.age;
=====================================================
function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;
}
// create your Person
var john = new Person();
john.first = 'pie';
john.last = 'kool';
john.age= 90;
console.log(john.bankBalance);
===================================================
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
// print hello in the3 different languages
for (var lang in languages) {
if (typeof languages[lang] === "string") {
console.log(languages[lang]);
}
}
====================================================
//Create the object called cashRegister
//and initialize its total property
var cashRegister = {
total: 0
};
//Using dot notation change the total property
cashRegister.total = 2.99;
====================================================
var cashRegister = {
total: 0,
add: function (itemCost) {
this.total += itemCost;
},
scan: function (item) {
switch (item) {
case "eggs":
this.add(0.98);
break;
case "milk":
this.add(1.23);
break;
case "magazine" :
this.add(4.99);
break;
case "chocolate":
this.add(0.45);
break;
}
return true;
}
};
//Scan 2 eggs and 3 magazines
cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan('magazine');
cashRegister.scan('magazine');
cashRegister.scan('magazine');
//Show the total bill
console.log('Your bill is '+cashRegister.total);
=====================================================
var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item,quantity) {
switch (item) {
case "eggs": this.add(0.98*quantity); break;
case "milk": this.add(1.23*quantity); break;
case "magazine": this.add(4.99*quantity); break;
case "chocolate": this.add(0.45*quantity); break;
}
}
};
cashRegister.scan("eggs",4);
cashRegister.scan("milk",4);
cashRegister.scan("magazine",4);
cashRegister.scan("chocolate",4);
console.log('Yourl is '+cashRegister.total);
=====================================================
function StaffMember(name,discountPercent){
this.name = name;
this.discountPercent = discountPercent;
}
var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);
// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("Xeno", 20);
var cashRegister = {
total:0,
lastTransactionAmount: 0,
add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},
scan: function(item,quantity){
switch (item){
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.99 * quantity); break;
case "chocolate": this.add(0.45 * quantity); break;
}
return true;
},
voidLastTransaction: function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
// Create a new method applyStaffDiscount here
applyStaffDiscount: function(employee) {
this.total -= (this.total*(employee.discountPercent/100));
}
};
cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object
// to applyStaffDiscount
cashRegister.applyStaffDiscount(me);
// Show the total bill
console.log('Your bill is '+cashRegister.total.toFixed(2));
======================================================
0 nhận xét:
Đăng nhận xét