0

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());  
sneak972
  • 11
  • 1
  • Why don't you just paste this into a console and see if it runs? Since you're only asking about *syntax*. – rvighne Feb 01 '14 at 23:12
  • 1
    By the convention, constructor functions names should start with an uppercase letter. – Wiktor Zychla Feb 01 '14 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](http://en.wikipedia.org/wiki/Prototype-based_programming) – bitWorking Feb 01 '14 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! :) – sneak972 Feb 01 '14 at 23:23
  • This question appears to be off-topic and belongs on http://codereview.stackexchange.com/ – DanMan Feb 01 '14 at 23:38
  • This answer should explain the basics of constructor functions and prototype:http://stackoverflow.com/a/16063711/1641941 – HMR Feb 02 '14 at 00:15

2 Answers2

1

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.

Manuel Hoffmann
  • 539
  • 1
  • 7
  • 23
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());
bitWorking
  • 12,485
  • 1
  • 32
  • 38