-1

I think I finally wrapped my head around understanding how methods, constructor functions, and objects work. Could someone please review my code, and let me know if I using the correct names and syntax? Thanks a ton!

function objectConstructor (arg1, arg2) {
     this.property1 = arg1;
     this.property2 = arg2;
     this.methodName = functionName;
}
function functionName() {
     console.log(this.property1 + ' ' + this.property2);   
}

var object1 = new objectConstructor('value1','value2');

console.log(object1.property1);
console.log(object1.methodName());  
6
  • Why don't you just paste this into a console and see if it runs? Since you're only asking about syntax. Commented Feb 1, 2014 at 23:12
  • 1
    By the convention, constructor functions names should start with an uppercase letter. Commented Feb 1, 2014 at 23:14
  • you didn't understand that javascript is a prototype based language and in the way you do it it's very memory intensive and slow. Prototype-based_programming Commented Feb 1, 2014 at 23:17
  • I agree and understand javascript is a prototype based language. This example was just for my own understanding on how everything works as a whole. I would never uses these names in a project or anything that would be insane! :) Commented Feb 1, 2014 at 23:23
  • This question appears to be off-topic and belongs on codereview.stackexchange.com Commented Feb 1, 2014 at 23:38

2 Answers 2

0

Use prototype functions. Else your inner functions get copied with every instance.

function Person(firstName, lastName)
{
    this.firstName = firstName;
    this.lastName = lastName;
}

Person.prototype.getFullName = function()
{
    return this.firstName+' '+this.lastName;
}

var person1 = new Person('foo', 'bar');
console.log(person1.getFullName());

There are many other patterns which for example prevent the pollution of the global scope or allow a more class like approach. (Object literal, Module Pattern, Self-Executing Anonymous Functions)

The same example with the module pattern:

var Person = (function()
{
    var Person = function(firstName, lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    Person.prototype.getFullName = function()
    {
        return this.firstName+' '+this.lastName;
    }

    return Person;

})();

var person1 = new Person('foo', 'bar');
console.log(person1.getFullName());
Sign up to request clarification or add additional context in comments.

1 Comment

@sneak972 I've add a module pattern example.
0

Methods of Javascript classes should be defined as prototype:

var CustomObject = function (arg1, arg2) {
     this.property1 = arg1;
     this.property2 = arg2;
};

CustomObject.prototype.functionName = function() {
     console.log(this.property1 + ' ' + this.property2);   
};

var object1 = new CustomObject("value1","value2");

Everything else seems fine to me though.

3 Comments

Messing with the Object prototype is considered bad practice.
You're absolutely right thanks Manuel! I'm still learning prototypes, but thank you for the example.
@DanMan Object should be the name of a custom type here. I forgot that there was the javascript-"Object".

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.