Home > Web Front-end > JS Tutorial > JavaScript combines constructor mode and prototype mode instances_javascript tips

JavaScript combines constructor mode and prototype mode instances_javascript tips

WBOY
Release: 2016-05-16 15:56:42
Original
1044 people have browsed it

The examples in this article describe how JavaScript uses the constructor pattern and the prototype pattern in combination. Share it with everyone for your reference. The details are as follows:

function testPrototype2(){
  function Person3(name, age, job){
    this.name=name;
    this.age=age;
    this.job=job;
    this.friends =["shelb", "court"];
  }
  Person3.prototype = {
    constructor:Person3,
    sayName:function(){
      alert(this.name);
    }
  }
  var person1 = new Person3("jack",10,"it");
  var person2 = new Person3("karry",1,"woker");
  person1.friends.push("tom");
  console.info(person1.friends);
  console.info(person2.friends);
  console.info(person1.friends==person2.friends);
  console.info(person1.sayName == person2.sayName);
}

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template