Object literal enhancements, classes, samples
satya - 12/24/2024, 3:44:44 PM
What are object literals
satya - 12/24/2024, 3:55:00 PM
A complex example
// External variables
const deviceType = "Smartphone";
const brand = "TechCo";
const serialNumber = 12345;
const featureBase = "feature";
// Initialize the object
const smartDevice = {
// Static properties
deviceType,
brand,
serialNumber,
// Dynamic property with a static key
releaseYear: 2024,
// Dynamic property with a computed key
[`${featureBase}_1`]: "Bluetooth Connectivity",
[`${featureBase}_2`]: "Wireless Charging",
// Function to show details
getDetails() {
return `Device: ${this.deviceType} (${this.brand}), Serial: ${this.serialNumber}`;
},
// Function to list features
listFeatures() {
return Object.entries(this)
.filter(([key]) => key.startsWith(featureBase))
.map(([_, value]) => value)
.join(", ");
},
// Static utilities
static: {
isValidSerial(serial) {
return typeof serial === "number" && serial > 0;
},
compareDevices(deviceA, deviceB) {
return deviceA.releaseYear > deviceB.releaseYear
? `${deviceA.deviceType} is newer than ${deviceB.deviceType}`
: `${deviceB.deviceType} is newer than ${deviceA.deviceType}`;
}
}
};
// Demonstrate the object
console.log(smartDevice);
// Example Output:
// {
// deviceType: 'Smartphone',
// brand: 'TechCo',
// serialNumber: 12345,
// releaseYear: 2024,
// feature_1: 'Bluetooth Connectivity',
// feature_2: 'Wireless Charging',
// getDetails: [Function: getDetails],
// listFeatures: [Function: listFeatures],
// static: { isValidSerial: [Function: isValidSerial], compareDevices: [Function: compareDevices] }
// }
console.log(smartDevice.getDetails());
// Device: Smartphone (TechCo), Serial: 12345
console.log(smartDevice.listFeatures());
// Bluetooth Connectivity, Wireless Charging
// Use static functions
console.log(smartDevice.static.isValidSerial(12345)); // true
console.log(smartDevice.static.isValidSerial("abc123")); // false
const deviceA = { deviceType: "Tablet", releaseYear: 2022 };
const deviceB = { deviceType: "Smartwatch", releaseYear: 2023 };
console.log(smartDevice.static.compareDevices(deviceA, deviceB));
// Smartwatch is newer than Tablet
satya - 12/24/2024, 3:56:23 PM
Note: The above is ChatGPT gnerated....
satya - 12/24/2024, 4:00:20 PM
The object literal
// An object literal: no function, or class used
// var smartDevice is just an object
const smartDevice = {
// properties from global
deviceType,
brand,
serialNumber,
// as a key value pair
releaseYear: 2024,
satya - 12/24/2024, 4:42:47 PM
Easy ones
satya - 12/24/2024, 4:43:58 PM
The dynamic object attribs or keys
// Dynamic property with a static key
releaseYear: 2024,
// Dynamic property with a computed key
[`${featureBase}_1`]: "Bluetooth Connectivity",
[`${featureBase}_2`]: "Wireless Charging",
satya - 12/24/2024, 4:46:27 PM
Commentary
satya - 12/24/2024, 4:49:08 PM
The darn commas...
satya - 12/24/2024, 4:52:40 PM
On Functions in an object literal
satya - 12/24/2024, 4:53:12 PM
Few functions
// Function to list features
listFeatures() {
return Object.entries(this)
.filter(([key]) => key.startsWith(featureBase))
.map(([_, value]) => value)
.join(", ");
},
// Static utilities
static: {
isValidSerial(serial) {
return typeof serial === "number" && serial > 0;
},
compareDevices(deviceA, deviceB) {
return deviceA.releaseYear > deviceB.releaseYear
? `${deviceA.deviceType} is newer than ${deviceB.deviceType}`
: `${deviceB.deviceType} is newer than ${deviceA.deviceType}`;
}
}
satya - 12/24/2024, 4:54:53 PM
Understanding [] in function parameters...
listFeatures() {
return Object.entries(this)
.filter(([key]) => key.startsWith(featureBase))
.map(([_, value]) => value)
.join(", ");
},
satya - 12/31/2024, 12:17:58 PM
Object.entries
satya - 12/31/2024, 12:19:27 PM
Example
const myObject = {
//attributes
name: "Sathya",
age: 30,
//functions
greet() {console.log("Hello!");},
listEntries() {
return Object.entries(this);
}
};
console.log(myObject.listEntries());
// Output:
// [
// ["name", "Sathya"],
// ["age", 30],
// ["greet", [Function: greet]]
// ]
satya - 12/31/2024, 12:30:42 PM
Other methods on an object
//Like a hash map or table
.keys // just the atttribute names
.values // values
.assign(targetObj, ...sourceObjects)
//assign the source object attributes to the target object
//and return the target
.freeze
//no additions, removals, or updates
.seal
//updates are allowed
.is(obj1, obj2)
// compare their values like ===
satya - 12/31/2024, 12:31:16 PM
Quick look
satya - 12/31/2024, 1:25:31 PM
On prototypes
satya - 12/31/2024, 1:37:31 PM
Constructor functions are different
satya - 12/31/2024, 1:38:30 PM
Lets get back to the [] in function parameters
listFeatures() {
return Object.entries(this)
.filter(([key]) => key.startsWith(featureBase))
.map(([_, value]) => value)
.join(", ");
},
satya - 12/31/2024, 2:08:11 PM
Few notes on []
satya - 12/31/2024, 2:23:57 PM
The filter method and calling vs called
satya - 12/31/2024, 2:33:08 PM
[key]....explanation
satya - 1/1/2025, 4:41:13 PM
More on prototypes....
satya - 1/1/2025, 4:45:49 PM
If I am able to define all methods on a cf, why define them on a prototype?
satya - 1/1/2025, 4:46:40 PM
A pattern: A refactored cf, Consider the following cf
function Person(name, age) {
this.name = name;
this.age = age;
// If methods are here, this can get messy!
this.sayHello = function () {
console.log(`Hello, I'm ${this.name}`);
};
this.getAge = function () {
return this.age;
};
}
satya - 1/1/2025, 4:47:17 PM
Comments
satya - 1/1/2025, 4:47:41 PM
Using a prototype instead
function Person(name, age) {
this.name = name; // Instance-specific
this.age = age; // Instance-specific
}
// Shared methods
Person.prototype.sayHello = function () {
console.log(`Hello, I'm ${this.name}`);
};
Person.prototype.getAge = function () {
return this.age;
};
satya - 1/1/2025, 4:48:51 PM
Note how the methods are extracted out
satya - 1/1/2025, 4:53:46 PM
The one-to-one aspect of a prototype to its cf
satya - 1/1/2025, 4:58:22 PM
When do you program the prototype?
satya - 1/1/2025, 5:00:36 PM
What does this cf time approach may look like?
satya - 1/1/2025, 5:00:52 PM
In other words a class in JS: cf + prototype :)
In other words a class in JS: cf + prototype :)
satya - 1/1/2025, 5:11:57 PM
Do object literals create cf functions or move them to the prototype?
satya - 1/1/2025, 5:23:58 PM
The strangeness of "this" of a prototype
satya - 1/1/2025, 5:31:20 PM
Pointers on variable scopes, and this
satya - 1/1/2025, 5:39:27 PM
Arrow functions and this
satya - 1/1/2025, 5:47:42 PM
Lets get complex a bit with "call" and inheritance, the old way
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
console.log(`Hello, my name is ${this.name}`);
};
function Student(name, grade) {
Person.call(this, name); // Initialize instance-specific properties
this.grade = grade;
}
// Inherit methods from Person
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
// Add Student-specific methods
Student.prototype.study = function () {
console.log(`${this.name} is studying for grade ${this.grade}`);
};
const alice = new Student("Alice", "A");
alice.greet(); // Output: Hello, my name is Alice
alice.study(); // Output: Alice is studying for grade A
satya - 1/1/2025, 5:49:31 PM
Notes
satya - 1/1/2025, 5:50:09 PM
ES6 brings this to the modern world
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
class Student extends Person {
constructor(name, grade) {
super(name); // Calls the parent constructor
this.grade = grade;
}
study() {
console.log(`${this.name} is studying for grade ${this.grade}`);
}
}
const alice = new Student("Alice", "A");
alice.greet(); // Output: Hello, my name is Alice
alice.study(); // Output: Alice is studying for grade A
satya - 1/1/2025, 5:50:34 PM
The code is like MOST other oo languages....
The code is like MOST other oo languages....
satya - 1/1/2025, 6:01:20 PM
Nature of Object.create
satya - 1/1/2025, 7:59:03 PM
What happens if 2 cfs share the same instance of a prototype
satya - 1/1/2025, 8:21:59 PM
Static functions in literals
satya - 1/1/2025, 8:33:31 PM
Really high level concepts covered in this article/exploration
satya - 1/1/2025, 8:35:30 PM
If I have to classify even more, the topics