最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Self-pushing object constructor - Stack Overflow

programmeradmin3浏览0评论

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.

Share Improve this question edited Jul 15, 2016 at 1:40 Robert Tan 6741 gold badge8 silver badges21 bronze badges asked Nov 24, 2013 at 0:19 fizzydrinksfizzydrinks 6441 gold badge7 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You 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.

发布评论

评论列表(0)

  1. 暂无评论