1
#1 (object) 
function Person(f){
      this.firstname = f;

      alert(this.firstname);
    }
    var me = new Person('benny');


#2 (function) 
    function Person(f){
       alert(f);
    }
    Person('benny');

Im new in js oop, I have knowledge of oop in PHP

My question is what's different between 1st one and 2nd one?

in php, if I create an obj, i will start with class{}

in JS, it seems like you can also create an obj var obj = {} OR like create it like a function?

can someone enplane how it works?

7

1 Answer 1

1

The first is a constructor function and the second is just a plain function with a capital letter.

The constructor function is a way of creating data types. JavaScript offers prototypal inheritance.

Person.prototype.newmethod = function(){...};

There is a class keyword in the new JavaScript version. You could use also TypeScript. It may make more sense to you.

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

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.