普通函数、构造函数

普通函数和构造函数的区别

  • this指向不同(构造函数的this指向创建的对象实例上)
  • 调用方式(构造函数 调用需要new)
  • 命名方式(构造函数首字母大写,普通函数使用驼峰法命名)

普通函数(驼峰命名 eg:personNew)

function add(a,b){
   retrun a+b;  
}
add(1,2);   
console.log(add(1,2));   //3

构造函数(必须大写)

function Person(name,age){
   this.name = name;
   this.age = age;
   this.todo = function(){
      return this.name; 
   }       
}
var aa = new Person('funny',20);
aa.name;
原文地址:https://www.cnblogs.com/zwtqf/p/9043492.html