0

I'm just started with JavaScript oops concept , I want to access 3 instance variable from a constructor with out 'this' keyword, First value is coming but not 2nd.. and so on .. `

        function CreateObject()
        {
            var p1 = new person('chanky',25,'Male');           
        }

        function person(name,age,sex)
        {
            this.name = name; 
            this.age = age;
            this.sex = sex;      

            document.write(name); //Working
            document.write(age); //not Working
            document.write(sex); //not Working

         /*If we use 'this' keyword then all are Working */

            document.write(this.name); // Working
            document.write(this.age); // Working
            document.write(this.sex); // Working
        }`
7
  • Are you getting any error? Commented Jul 30, 2015 at 10:38
  • when you are ommiting 'this' then you refer to argument of person function and with 'this' to instance property. you cannot mix these two to refer always to instance property Commented Jul 30, 2015 at 10:39
  • You don't refer instance variables, you refer constructor arguments (name,age,sex), this is not the same Commented Jul 30, 2015 at 10:40
  • but that works...fiddle Commented Jul 30, 2015 at 10:40
  • It work for me in both cases. Commented Jul 30, 2015 at 10:44

3 Answers 3

1

What's the problem with 'this'? You want to access a member that you not declared, so you use 'this'.

You can access the values outside the class like this:

var p = new person('chanky',25,'Male');
alert (p.age);

function person(name,age,sex)
        {
            this.name = name; 
            this.age = age;
            this.sex = sex;      
        }

If you're still worried about, declare a variable and assign the values you've received to them, and as you're using an OO approach, encapsulate the variables to access them outside of your class

function person(name,age,sex)
        {
            var _name;
            var _age;
            var _sex;

            _name = name; 
            _age = age;
            _sex = sex;      

            this.getName = function () {
                return _name;
            };

            this.getAge = function () {
                return _age;
            };

            this.getSex = function () {
                return _sex;
            };
        }

And use the 'methods':

var p2 = new person('chanky',25,'Male');
alert (p2.getAge());
Sign up to request clarification or add additional context in comments.

Comments

0

Hello there Chanky this should work fine

I have just created a jsFiddle to show using console.log

jsFiddle : https://jsfiddle.net/hq4ebs2d/

Javascript

function person(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;

    console.log(name); //Working
    console.log(age); //not Working
    console.log(sex); //not Working

    /*If we use 'this' keyword then all are Working */

    console.log(this.name); // Working
    console.log(this.age); // Working
    console.log(this.sex); // Working
};

var p1 = new person('chanky', 25, 'Male');

Just one quick note you have }` at the end of your code, make sure to change that to };

Comments

0

Please see the jsbin.. https://jsbin.com/teyupuhele/edit?html,js,output

This is best practice for OOP...

function person(name, age, sex) {
  this.name = name;
  this.age = age;
  this.sex = sex;


};

// create p1 instance of object person

var p1 = new person('chanky', 22, 'Male');

console.log(p1.name); //Working
console.log(p1.age); //Working
console.log(p1.sex); //Working

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.