Results 1 to 1 of 1

Thread: What is the difference between them and which one is more recomended?

  1. #1
    Join Date
    Jun 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is the difference between them and which one is more recomended?

    Hey,

    The following two constructor functions do the same job. But what is the difference between them and which one is more recomended?

    First function

    Code:

    Code:
    // we define a constructor for Person objects
    function Person(name, age, isDeveloper) {
        this.name = name;
        this.age = age;
        this.isDeveloper = isDeveloper || false;
    
        this.writesCode = function() {
          console.log(this.isDeveloper? "This person does write code" : "This person does not write code");
        }
    }
    
    var person1 = new Person("Bob", 38, true);
    var person2 = new Person("Alice", 32);
    
    // prints out: This person does write code
    person1.writesCode();
    // prints out: this person does not write code
    person2.writesCode();
    Second function
    Code:

    Code:
    // we define a constructor for Person objects
    function Person(name, age, isDeveloper) {
        this.name = name;
        this.age = age;
        this.isDeveloper = isDeveloper || false;
    }
    
    // we extend the function's prototype
    Person.prototype.writesCode = function() {
        console.log(this.isDeveloper? "This person does write code" : "This person does not write code");
    }
    
    var person1 = new Person("Bob", 38, true);
    var person2 = new Person("Alice", 32);
    
    // prints out: This person does write code
    person1.writesCode();
    // prints out: this person does not write code
    person2.writesCode();
    Last edited by keyboard; 06-18-2018 at 02:10 AM. Reason: Format: Code Tags

Similar Threads

  1. Difference between PPC and CPC
    By ashleygreen in forum General Paid Work Requests
    Replies: 1
    Last Post: 08-29-2013, 03:14 PM
  2. Resolved & and && difference
    By neo_philiac in forum PHP
    Replies: 1
    Last Post: 08-25-2009, 08:52 PM
  3. What's the difference between <td> and <th> tag?
    By heavensgate15 in forum HTML
    Replies: 7
    Last Post: 06-28-2009, 02:10 PM
  4. Replies: 3
    Last Post: 06-27-2007, 09:02 PM
  5. difference gif - jpg
    By chechu in forum Graphics
    Replies: 7
    Last Post: 11-17-2006, 03:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •