javascript对象 以及对象属性深入学习

/**
 * @desciption
 * @author Created by royarn
 * @time 2017/9/28 0028
 */

//函数调用  --object类型
function displayInfo() {
  //arguments为传入参数集合   arguments对象为数组
  console.log(arguments);
  if(typeof arguments[0].name == "string") { //类型检查
    console.log("yeah , you are right")
  }

  if(typeof arguments[0].age == "string") {
    console.log("the age is : " + arguments[0]['age']);
  }

  if(typeof arguments[0].age == "undefined") {
    console.log("the wrong age ! maybe you never input it");
  }
}

//displayInfo({name: "lizq", age: "23"});  //适合传入大量可选参数

//displayInfo({name: "nichlos"});

//Array类型  --数组内部数据为弱类型  数组上每一位置存储数据可以为多种类型  这点与其他语言的数组存储类型有区别
var listWithNew = new Array("1", 2, {name: "lizq"});
//console.log(JSON.stringify(listWithNew));

var listWithoutNew = Array('1', 2, {name: "lizq"});
//console.log(JSON.stringify(listWithoutNew));

//特别说明,数组的长度 length不是只读的,可以通过length更改数据长度
listWithoutNew.length = 2;
//console.log(JSON.stringify(listWithoutNew) + "===========" + listWithoutNew[2]);


//对象检查  对象检查一般检查具体实例是否属于其对应的对象
if(listWithoutNew instanceof Array) {
  //console.log("instanceOf used to check the object type !!! and remember it!");
}

//transform method
//console.log(listWithoutNew.toLocaleString() + "//////" + listWithoutNew.toString() + "=========" + listWithoutNew.valueOf());

//数组的一种栈式存储结构,保证数据后进先出 Last-In-First-Out
var stackList = Array();
stackList.push("stack array");
stackList.push(2);
stackList.push({name: "lizq"});
//then pop one of them
// console.log("the array output : " + JSON.stringify(stackList.pop()));
// console.log("the array output : " + JSON.stringify(stackList.pop()));
// console.log("the array output : " + JSON.stringify(stackList.pop()));

//数组的一种队列存储结构,保证数组先进先出 First-In-First-Out  shit()保证先插入的数据先输出 unshift() 始终向数组队列队首插入数据 并返回新数组长度
// console.log("the array output : " + JSON.stringify(stackList.unshift({age: "23"})));
// console.log("the array output : " + JSON.stringify(stackList));

//排序函数 sort()  为参数时输出默认排序结果  也可以传入一个function自定义排序
var list = [20,2,1,3,9,8,0];

//eval函数  可以传入一个函数名字符串  可以以字符串的形式传入调用函数
var msg = "the eval function can use the vriable";
function useEval() {console.log("it can be userd for function")};
//eval("useEval");

//eval函数也可以在内部已字符串定义函数,在包含环境内可以直接调用
eval("function evalFn () { console.log('it can be transformed the real function')}");
//evalFn();

//在web浏览器中,全局对象Global作为window对象的部分属性   在web中  window为一切对象或属性之父
//而在javascript的内置对象系列中,Global作为全部对象,诸如Object Array String Function Math  RangError RefrenceError 等等均为Global属性
// var color = "shadow";
// function getColor() {
//   console.log(window.color);
// }
// window.getColor();

//属性配置  如configurable   enumerable   writable  value  --此为数据属性
//配置person的name属性是否可更改  默认值


// "use strict";
var person = {};
Object.defineProperty(person, "name", {
  configurable: false, //是否可删除此属性
  writable: true, //是否可更改
  value: "Nicholes" //默认值
});

// Object.defineProperty(person, "name", {
//   configurable: true,
//   value: "Nicholes"
// });
//console.log(person.name);
person.name = "lizq"; //更改属性值
//console.log(person.name);
delete person.name; //删除name属性  因为configurable配置为false 属性不可删除
// console.log(person.name);

//属性配置  --访问器属性  包含configurable   enumeable   get  set
//我的理解: 数据属性在实际的应用场景中多为对象内部真实属性,多为直接设置,写入和读取的值保持一致
// 而访问器属性多为侵入式修改   通过set来影响其他数据属性的值
var book = {
  _year: 2004,
  edition: 1
};
Object.defineProperty(book, "year", {
  get: function () {
    return this._year;  //this refer to the real object --book
  },
  set: function (newValue) {
    if(newValue > 2004) {
      this._year = newValue;
      this.edition = this.edition + newValue - 2004;
    }
  }
});
book.year = 2017;
//console.log(book.edition + "=================" + book._year);

//为对象定义多属性  --其中既包含数据属性 也可包含访问器属性
var bookAttr = {};
Object.defineProperties(bookAttr, {
  _year: {
    value: 2004
  },
  edition: {
    value: 1
  },
  year: {
    get: function () {
      return this._year;
    },
    set: function (newValue) {
      if(newValue > 2004) { //set值时更改其他属性值
        this._year = newValue;
        this.edition += newValue -2004;
      }
    }
  }
});
bookAttr.year = 2017;
//console.log(bookAttr._year + "=====================" +bookAttr.edition + "----------" + bookAttr.year);

//获取各属性相关配置描述  如数据属性的configurable witable enumeable value 访问器属性的configurable wirtable set get
//var descriptor = Object.getOwnPropertyDescriptor(bookAttr, "_year");
var descriptor = Object.getOwnPropertyDescriptor(bookAttr, "year");
console.log(JSON.stringify(typeof descriptor.get));

猜你喜欢

转载自blog.csdn.net/lzqworkonline/article/details/78133953