COMP 1537 - Week 5 - JavaScript and Objects
COMP 1537 - Week 5 - JavaScript and Objects
JS OBJECTS First-class citizenship, within the world of programming, means that a given entity (such as a function) supports all the
operational properties inherent to other entities; properties such as being able to be assigned to a variable, passed
around as a function argument, returned from a function, etc. Basically, first-class citizenship simply means “being
able to do what everyone else can do.”
Revisiting JS objects:
JS objects:
Are associative arrays: key-value pairs for each property added to an object
Do not require classes (at least prior to ES6)
Can be functions – as functions are objects too!
JS functions are objects
JS functions are “first class citizens”
Which, within the context of JS as a programming language, means:
Functions can be passed as arguments to other functions
This is a core aspect of JS
Because JS functions are objects, they can have properties – and do!
JS functions always have an arguments array property
So you can see all of the arguments passed into the function
Using ‘let’:
Limited within the block it is defined in
In a function, a loop (inside of the loop block or parenthesis)
Cannot be re-declared
When creating JavaScript function objects, we can use the keyword ‘this’,
example:
function User (n) {
this.name = n;
this.toString = function() {
return this.name;
}
}
With this:
let obj = {};
console.log(obj);
We get:
console.log(Student.prototype); // object
console.log(s1.prototype); // undefined
console.log(s1.__proto__); // object
However, most current languages allow us to use some shortened syntax for
defining lambdas
E.g., Java:
button.addActionListener(e -> System.out.println("Pressed"));