Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:23:22+00:00 2026-06-03T11:23:22+00:00

Is this code, function Person() { function myMethod() { alert (‘hello’); } this.method =

  • 0

Is this code,

function Person() {
    function  myMethod() {
        alert ('hello');
    }
    this.method = myMethod;
}

equivalent to:

function Person() {    }
Person.prototype.method2  = function() {
    alert ('hello');
};

If yes, which method definition should I use and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:23:23+00:00Added an answer on June 3, 2026 at 11:23 am

    They are functionally equivalent in your simple example, but behind the scenes work very differently. The prototype property on a function is really the “prototype template”. It says “whenever an object is made and I am used as the object’s constructor, give them this object as their prototype”.

    So all Persons created in your second example share the same copy of the method2 method.

    In the first example, each time the interpreter encounters the function keyword, then it creates a new function object. So in the first example, each instance of Person has their own copy of the myMethod method. The vast majority of the time this doesn’t matter. But this first approach uses more memory, and sometimes that does matter.

    They are not functionally equivalent in more interesting cases. In the first example, myMethod can access local variables defined in Person, but the second example cannot, as one difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: function Person(){ this.age = 30; } function Stats(){ this.age
I have this code here: var Person = (function() { var name; var PersonConstructor
Hello I get a weird error in this code: function validateForm(aform) { var s=;
i have this code $(function() { $('#ans_vote a span').click(function(){alert('working');return false;});}); and this html <div
With this code function someFunction(classParam:Class):Boolean { // how to know if classParam implements some
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left
I got this code: function zeroPad(num, places) { var zero = places - num.toString().length
I have just come across this code: function test(){ //... if ( $profilerule ==
I have this code: _trackit: function(){ for(var key in this.items.sublinks){ switch(key){ case 'shoes': for(var
This code does not function as expected: // $field contains the name of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.