So JavaScript is a functional language, classes are defined from functions and the function scope corresponds to the class constructor. I get it all now, after a rather long time studying how to OOP in JavaScript.
What I want to do is not necessarily possible, so I first want to know if this is a good idea or not. So let's say I have an array and a class like the following:
var Entry = function(name, numbers, address) {
this.name = name;
if(typeof numbers == "string") {
this.numbers = [];
this.numbers.push(numbers);
}
else this.numbers = numbers;
this.address = address;
};
var AddressBook = [];
And I add contacts with the following function:
function addContact(name, numbers, address) {
AddressBook.push(new Entry(name, numbers, address));
}
Can't I make it so new Entry()
would put itself into AddressBook
? If I can't do this on create, it would be interesting to do it with a method in the Entry
prototype as well. I couldn't figure out a way to do anything similar.
So JavaScript is a functional language, classes are defined from functions and the function scope corresponds to the class constructor. I get it all now, after a rather long time studying how to OOP in JavaScript.
What I want to do is not necessarily possible, so I first want to know if this is a good idea or not. So let's say I have an array and a class like the following:
var Entry = function(name, numbers, address) {
this.name = name;
if(typeof numbers == "string") {
this.numbers = [];
this.numbers.push(numbers);
}
else this.numbers = numbers;
this.address = address;
};
var AddressBook = [];
And I add contacts with the following function:
function addContact(name, numbers, address) {
AddressBook.push(new Entry(name, numbers, address));
}
Can't I make it so new Entry()
would put itself into AddressBook
? If I can't do this on create, it would be interesting to do it with a method in the Entry
prototype as well. I couldn't figure out a way to do anything similar.
2 Answers
Reset to default 7You could try passing the AddressBook
array reference to the function like such:
var Entry = function(name, numbers, address, addressBook) {
this.name = name;
if(!(numbers instanceof Array)) {
this.numbers = [numbers];
}
else this.numbers = numbers;
this.address = address;
addressBook.push(this);
};
var addressBook = [];
function addContact(name, numbers, address) {
new Entry(name, numbers, address, addressBook)
}
Reconsider the design approach. The "Entry" object's primary role is Information Holder. You should encapsulate the AddToAddressBook functionality in some sort of a controller or eventHandler.
Details You've got more than 1 responsibility and it's generally not a good idea to tightly couple the 2 concerns. (e.g. Design principles involved Single Responsibility Principle and Separation of Concerns.)
Information holder – an object designed to know certain information and provide that information to other objects.
Structurer – an object that maintains relationships between objects and information about those relationships.
I'd suggest reading up on SOLID principles and try to keep each object's responsibilities narrowly focused. Your code will be less plex and easier to maintain and extend going forward.
Check out - http://aspiringcraftsman./series/solid-javascript/
There's a example of products and cart which is pretty close to your scenario above.