Home  >  Article  >  Web Front-end  >  Simple implementation method of javascript encapsulation_javascript skills

Simple implementation method of javascript encapsulation_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:45:371397browse

The example in this article describes the simple implementation method of javascript encapsulation. Share it with everyone for your reference. The details are as follows:

var Person = function(name, gender, age) {
  this.Name = name;
  this.Gender = gender;
  this.Age = age;
  this.SetName = function(sname) {
    this.Name = sname;
  }
  this.GetName = function() {
    return this.Name;
  }
  this.SetGender = function(sgender) {
    this.Gender = sgender;
  }
  this.GetGender = function() {
    return this.Gender;
  }
};
/*
静态公用方法
*/
Person.Play = function() {
  alert("这是一个静态方法");
}
/*
Javascript规定,每一个构造函数都有一个prototype属性,指向另一个对象。这个对象的所有属性和方法,都会被构造函数的实例继承。
这意味着,我们可以把那些不变的属性和方法,直接定义在prototype对象上。
*/
Person.prototype.Address = "中华人民共和国";
Person.prototype.SayHello = function() {
  alert(Person.prototype.Address);
}

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

Statement:
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