2016/4/19

构造函数就是类

面向对象:

继承

封装

多态

构造函数也叫工厂方式

工厂方式么有new

每个函数有自己的函数极其浪费资源每一次new一次,所以函数对象不断在新的诞生

this有的时候会失效

1.函数前面有new的时候

function createPerson(name,sex)
{
 var obj=new Object();
 obj.name=name;
 obj.sex=sex;
 obj.showName=function()
 {
 alert(this.name)
 }
 obj.showSex=function()
 {
 alert(this.sex);
 }
 return obj;
}
var p1=createPerson('hu','man');
p1.showName();

  

function createPerson(name,sex)
{
  
 this.name=name;
 this.sex=sex;
 this.showName=function()
 {
 alert(this.name)
 }
 this.showSex=function()
 {
 alert(this.sex);
 }
  
}
var p1= new createPerson('hu','man');
p1.showName();

PTOTOTYPE

  节省资源

String.prototype.trim=function()
{
alert(this.replace(/^s+|s+$/g,'')) ;
};
var a=' adaf ';
a.trim();

默认规定构造函数的首字母需要大写,在上面 的例子中createperson就是就是构造函数。

原文地址:https://www.cnblogs.com/hduhdc/p/5407315.html