0

I'd like to get _firstName and _birthDate from : I tried https://jsfiddle.net/0xLqhufd/

var Person = (function () {
function Person(firstName, birthDate) {
    this._firstName = firstName;
    this._birthDate = birthDate;
}
return Person;
}());

alert(Object.keys(Person.Prototype));
3
  • You've to create new instance of the Class/function Commented Apr 4, 2016 at 11:26
  • 1
    You aren't currently using the prototype as far as I'm aware. To see the properties of an object you would have to look at the keys of an instance. The protype normally only contains methods or static variables. Commented Apr 4, 2016 at 11:27
  • 2
    _firstName and _birthDate aren't properties of Person.prototype (and it's prototype, lower case, not Prototype). Commented Apr 4, 2016 at 11:28

3 Answers 3

3

You cannot get an object's properties before creating a new instance.

Object.keys(new Person)

Also for your understanding, you have properties with your current instance not in prototype. At this moment your prototype will be empty and it inherits the Object's, The core one, that holds toString etc. And those are non enumerable, so you cannot get those by using Object.keys().

DEMO

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

2 Comments

"At this moment your prototype will be the core one" No, it won't. It'll be an object with no properties of its own other than constructor, which inherits toString and such from its prototype.
@T.J.Crowder Yes the Object's prototype. I meant to say that. Let me correct my words there
2

This is factory function. You need to create instances of the class using new.

var Person = (function () {
    function Person(firstName, birthDate) {
        this._firstName = firstName;
        this._birthDate = birthDate;
    }
    return Person;
}());

var tushar = new Person('Tushar', '15464');
console.log(tushar); // { _firstName: 'Tushar', _birthDate: '15464' }

Comments

1

You need to create a new person:

var newPerson = new Person('Lars', 'Svensson');

console.log(newPerson.firstname);
console.log(newPerson.birthDate);

This will allow you to access it's properties.

Comments

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.