How to add objects

Add to

  • Such as: the use of an object called a description 狗蛋of the man, first declare an object literal, to give the object properties and methods of evaluation;
var obj = {};


// 对象.属性 = 值;
obj.name = '狗蛋';// 名字是一个人的特征

// 在对象上的任何方法内都可以获取,和设置;

// 对象.方法名 = function(){}
// 注意:该方法后面的的赋值为一个函数,函数在什么时候执行?调用的时候才会执行;
obj.sayName = function(){  // 人会说出自己的名字,也就是人有自己的行为
	console.log('你好,我叫' + obj.name);
}
  • When the object is initialized by the literal, initialization properties
// 描述一个学生
var student = {
  name : '狗蛋',
  age : 12,
  gender : '男',
  sayName : function(){
    console.log(student.name);
  }
}
// 
  • A property and a value called the key-value pair . A plurality of key-value property for the approach used between commas, bond:
// 对象[属性名]  属性名必须是String类型,里面可以写字符串;
var obj = {};
obj['name'] = '狗蛋';
obj['sayName'] = function(){
  console.log('你好,我叫' + obj['name']);
}
Published 68 original articles · won praise 0 · Views 1299

Guess you like

Origin blog.csdn.net/zmmsdk/article/details/102579202