[]
are {}
then what's the difference (instanceof
){}
(Constructors)this
with regular objectsThere's been still some confusion about "what is a function", "what does a method mean", "what does property mean". So, if you ask any of those questions and can't answer quickly, then read:
Once again, try reading the Eloquent Javascript chapters from the last two days:
Tonight you will be working on the prototype exercise:
Setup for tonight's HW
- Create new folder
4.3-prototypes
- Initialize git in this new project
- Copy
constructors.js
- Create
.jscsrc
and set things up for Airbnb code style- Commit to
master
- Create github repo (
hub create
)- Push
- Switch to
develop
- Succeed!
Since Slack isn't permanent I will try to start copying some of the most helpful questions and answers from the slack rooms:
// Create a constructor called `KeepSecret`. The constructor function
// itself should accept a parameter called `secret` it should keep this
// as a private variable. Add a function to `KeepSecret` that is called
// `squeal` that returns the secret string.
// Create a constructor called `Key`. Create another constructor
// called `Safe`. Make the Safe constructor take 2 arguments. The
// first argument can be any piece if data to keep safe. This must
// be stored using a private variable like you did with KeepSecret.
// The 2nd param to the `Safe` constructor needs to be an instance
// of `Key` you need to store it as a private variable. Then, add a
// function to the Safe prototype called `unlock` that accepts a key.
// If the key matches the key that was used to create the Safe; then
// return the secret data.
property
?When we talk about a property
all we mean is that there is a value on an object.
If I want a property
called say
on a Dog equal to life if rough
, then I should be able to run var d = new Dog()
and when I check the value say
it should always return life is rough
.
method
?When we talk about a method
that just means that there is a function available on an object.
So if an assignment says that a Cat should have a method growl
, I should be able to create a Cat with var c = new Cat()
and then run c.growl()
.