1

I have this function here, I need to work with this other function called OrderRepository in another file.

main.js

function main() {

var orderRepo = new OrderRepository();

// Your code here

}

orderrepository.js

function OrderRepository() {

}

OrderRepository.prototype.getYesterdaysOrders = function 
  getYesterdaysOrders() {
  var yesterdaysOrders = [{ array of objects }],

 return yesterdaysOrders;
};

These were given as examples to use, 1 question rolled into 2 parts:

1) var orderRepo = new OrderRepository();

Can you initialize a function like this?

2) In the orderrepository.js:

function OrderRepository() {

}

is being called in main.js, but nothings inside of it, this was given as-is in the assignment, is this just a typo and really they meant to throw everything inside that function or am I missing something?

Shouldn't it look like this?

Expected

function OrderRepository() {

OrderRepository.prototype.getYesterdaysOrders = function 

  getYesterdaysOrders() {

  var yesterdaysOrders = [{ array of objects }],

  return yesterdaysOrders;

 };
}
1
  • 1
    Tips for asking questions: make your code as relevant as possible and as short as possible while displaying your problems. Commented Sep 8, 2019 at 1:26

2 Answers 2

1

Diving deeper into JavaScript, the language is hard to understand. JavaScript is not really OOP (imho), at least it does not implement the common OOP concept. Some call it object-based language. There are no classes. Recent ECMA Script standards do implement the class keyword, however, it is syntax sugar. Used with the new keyword it builds the same objects as you can achieve by 'constructor' functions.

Everything in JavaScript is an object, even numbers and functions. Every function can act as constructor function. The new keyword call a constructor function with a newly creates empty object as the function's this context. The function can do what it wants. If it does not return anything, its this context is returned by the new expression.

Since there are no classes, there is no inheritance. Some inheritance-like behavior is achieved by the prototype concept. In most cases the constructor will return nothing and sometimes modify the this object by adding properties. Methods are properties holding a function object. The object in the new context of a constructor call will have a prototype object reference as the __proto__ property. This is copied by the new operator from the prototype property of the called constructor function. The default is an empty object.

// empty constructor function
function OrderRepository() {

}

// define a function property on the constructor's prototype object
OrderRepository.prototype.getYesterdaysOrders = function 
  getYesterdaysOrders() {
  var yesterdaysOrders = [ /* array of objects */ ],

 return yesterdaysOrders;
};

// create an empty object with a `__proto__` property holding a
// reference to the object { getYesterdaysOrders: function(){/*code*/} }
var obj = new OrderRepository();

Now, when the method invocation obj.getYesterdaysOrders() is tried, JavaScript will look if there is such a property defined in obj. If not, it looks if there is a reference in obj.__proto__ and the property name is searched the properties of obj.__proto__. If not, the same step is repeated until it was found or the __proto__ property in the chain is null. Since obj.__proto__.getYesterdaysOrders is defined, it is checked if it is a callable function object and finally invoked with a this context of obj since we called obj.getYesterdaysOrders(). Otherwise an error is thrown.

NOTE: Even if the major browsers do expose the __proto__ property, it is not part of the standards. Do not use it directly (except for debugging at development time) and even more important: do not manipulate it. If you really need to get or manipulate a prototype (__proto__) of an object instance after construction, use the methods of the builtin Object object.

Upon your last edit: Your expected code would define a new function object in prototype on each instantiation (and thus constructor invocation). This is not what you want, it's just needless overhead.

Sign up to request clarification or add additional context in comments.

4 Comments

So, that empty constructor function is there by design and not an error, along with the rest of the code?
@mph85 Exactly. It creates an empty object with no individual properties. However, all the instances created by new share the function object, i.e. the method. Methods can even dynamcally create properties later as long as the object is not sealed. (see Object methods in documentation)
@mph85 In other words: All instances share the same prototype - predefined in prototype of the constructor function and copied to __proto__ of the instance. The method is defined in the shared prototype. Also notice that a prototype is always an instance, not a constructor. So a common pattern to replace a base prototype is: ConstructorFunc.prototype = new ProtoConstructorFunc();
@mph85 Open the DevTool's console in your browser and type OrderReository. It should show you a function object (after running the code in your question on top level). You can inspect it by clicking on it. Type OrderReository.myProp = 123; Try OrderReository again and you will find your new property - and prototype as well.
1

Regarding the first part, it looks like this assignment is dealing with how to create instances of objects in JavaScript as that is what the new operator does.

You can find more information regarding new on the MDN.

Regarding the second question, it is dealing with how to create objects that inherit methods. So, yes it is empty, until you get to the prototype expression.

The expected code would not give you the inheritance in this case. Notice how OrderRepository is repeated inside the function, which would be invalid. Javascript requires you to add inheritance to the special prototype property. Code that is added to the function declaration, would be scoped to the function only in that case.

You can find more information about prototype on the MDN.

3 Comments

thanks, ive read that, so a function is essentially an object so you should be able to initialize it, but what about my 2nd question, am i seeing something wrong?
so, shouldn't the 2nd part be inside that OrderRepository function?
Updated answer for clarification. It can be a bit confusing as inheritence is certainly an advanced topic for JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.