0

Given the following object:

function MyObject() {
  window.myFunc1 = this.myFunc1;
  window.myFunc2 = this.__proto__.myFunc2;
}

MyObject.prototype.anotherFunc = function () {
  window.myFunc3 = this.myFunc3;
  window.myFunc4 = this.__proto__.myFunc4;
}

MyObject.prototype.myFunc1 = function () { console.log(1); }

MyObject.prototype.myFunc2 = function () { console.log(2); }

MyObject.prototype.myFunc3 = function () { console.log(3); }

MyObject.prototype.myFunc4 = function () { console.log(4); }

var o = new MyObject();
o.anotherFunc();
window.myFunc1();
window.myFunc2();
window.myFunc3();
window.myFunc4();

In fiddle I'm getting what is expected, but when testing with an ios app under ionic framework the results (tested with safari debugger) are:

  window.myFunc1 -> undefined
  window.myFunc2 -> it works!
  window.myFunc3 -> it works!
  window.myFunc4 -> it works!

Isn't the prototype attached to the object before it is created?

EDIT

attached fiddle and working example, fine tuned the question

1
  • Uh, are you actually calling MyObject and anotherFunc anywhere? Commented Oct 22, 2014 at 20:13

2 Answers 2

1

It may happen if you create your object in an unusual way, and this is bound to something different from what you expect. In my case I was exporting a constructor from the node module, and creating an object with new require('module')();, which had the same effect of prototype functions being undefined.

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

Comments

0

I'm getting the following results […]

I cannot reproduce that. Try this:

function MyObject() {
  console.log(this.myFunc1);
  console.log(Object.getPrototypeOf(this).myFunc2);
}
MyObject.prototype.anotherFunc = function () {
  console.log(this.myFunc3);
  console.log(Object.getPrototypeOf(this).myFunc4);
}

MyObject.prototype.myFunc1 = function () { }
MyObject.prototype.myFunc2 = function () { }
MyObject.prototype.myFunc3 = function () { }
MyObject.prototype.myFunc4 = function () { }

var o = new MyObject();
o.anotherFunc()

Isn't the prototype attached to the object before it is created?

Uh, no. The prototype cannot really be attached to a non-existing object :-) Best think of it as happening simultaneously, that an object is created with a prototype, it doesn't exist without one.

What you might have meant that the object (with its prototype) is created before the constructor is executed - yes.

7 Comments

Then I'm not understanding why this.myFunc1 in object creator is undefined
It was not. Your example is unreproducable. Maybe you can create a fiddle or so that demonstrates the issue?
Well I was doing so when I realized that in fiddle it works right. I was testing it in an ios ionic app (angular) in the safari debugger I have always seen window.myFunc1 is undefined...
Maybe you had a typo in one of the three places where the identifier myFunc1 was used?
|

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.