创建object对象的几种方法,以及存值取值

//  创建空对象
var obj = {}; 
//  存值两种方式
obj[name] = "张三丰";
obg.name = "张三丰";
//  取值两种方式
var name =obj.name;
var name = obj[name];
//使用new操作符后跟Object构造函数
var person = new Object();  
 person.name = "kitty";
 person.age = 25;
//  使用“对象字面量”表示法
var person = {
      name : "kitty",
      age:25
 };
//  工厂模式法:
 var createPerson = function(name,age,job){
       var person = new Object();
       person.name = name;
       person.age = age;
       person.job = job;
       person.sayName = function(){
          alert(person.name);
       }
       return person;
    }
    var person1 = createPerson("zh","62","Doctor");
    person1.sayName();
//  构造函数模式法
function Person(name,age,job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.sayName = function(){
       alert(this.name);
    };
  };
  var person1 = new Person("zhou",23,"test");
  person1.sayName();

 

posted @ 2020-03-26 17:32  万里哥  阅读(2546)  评论(0编辑  收藏  举报