最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JS克隆,屬性,數(shù)組,對象,函數(shù)實例分析

 更新時間:2016年11月26日 11:37:52   作者:牛逼的霍嘯林  
這篇文章主要介紹了JS克隆,屬性,數(shù)組,對象,函數(shù),結(jié)合實例形式分析了javascript中面向?qū)ο蟪绦蛟O計相關的對象、屬性、函數(shù)及數(shù)組等相關技巧,需要的朋友可以參考下

本文實例講述了JS克隆,屬性,數(shù)組,對象,函數(shù)。分享給大家供大家參考,具體如下:

<script type="text/javascript">
/* 克隆原型得到對象 */
function clone(object) {
  function F() {}
  F.prototype = object;
  return new F;
}
var Person = {
 name: 'default name',
 getName: function() {
  return this.name;
 }
};
var reader = clone(Person);
console.log(reader.getName()); // This will output 'default name'.
reader.name = 'John Smith';
console.log(reader.getName()); // This will now output 'John Smith'.
/* Author Prototype Object. */
var Author = clone(Person);
Author.books = []; // 書數(shù)組
Author.getBooks = function() {
 return this.books;
}
var author = [];
author[0] = clone(Author);
author[0].name = 'Dustin Diaz';
author[0].books = ['JavaScript Design Patterns'];
author[1] = clone(Author);
author[1].name = 'Ross Harmes';
author[1].books = ['JavaScript Design Patterns','PHP','Mysql'];
console.log(author[0].getName());
console.log(author[0].getBooks());
console.log(author[1].getName());
console.log(author[1].getBooks());
</script>

結(jié)果

這里的console.log很有意思,比alert有意思,alert不能獲取全部數(shù)據(jù),需要一個個彈出。

js的數(shù)組定義也很有意思。

進一步升級

<script type="text/javascript">
/* 克隆原型得到對象 */
function clone(object) {
  function F() {}
  F.prototype = object;
  return new F;
}
var Person = {
 name: 'default name',
 getName: function() {
  return this.name;
 }
};
var Author = clone(Person);
Author.books = []; // 書數(shù)組
Author.getBooks = function() {
 return this.books;
}
var authorClone = clone(Author);
console.log(authorClone.name); // string 'default name'.
authorClone.name = 'new name'; // 重新賦值
console.log(authorClone.name); // Now linked to the primative authorClone.name, which
// is the string 'new name'.
console.log(Author.getName()); // 沒有改變,任然是 'default name'
console.log(Author.getBooks()); // 空的
authorClone.books.push('new book'); // Author被改了
authorClone.books.push('new new book'); // Author被改了
console.log(Author.getBooks()); // array 'new book'
console.log(authorClone.getBooks()); // array 'new book'
authorClone.books = []; // 定義了屬于自己的books數(shù)組
authorClone.books.push('new book2'); // We are now modifying that new array.
authorClone.books.push('new book3');
authorClone.books.push('new book4');
console.log(authorClone.getBooks());
console.log(Author.getBooks());
var CompoundObject = {
 string1: 'default value',
 childObject: {
  bool: true,
  num: 10
 },
 getChild: function() { // 返回對象Object
  return this.childObject;
 }
}
var compoundObjectClone = clone(CompoundObject);
compoundObjectClone.childObject.num = 5; // 不好的方式
compoundObjectClone.childObject = { // 好一點的方式
 bool: true,
 num: 5
};
console.log(compoundObjectClone.getChild());
</script>

結(jié)果:

更多關于JavaScript相關內(nèi)容可查看本站專題:《JavaScript常用函數(shù)技巧匯總》、《javascript面向?qū)ο笕腴T教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學運算用法總結(jié)

希望本文所述對大家JavaScript程序設計有所幫助。

相關文章

最新評論

新巴尔虎左旗| 文安县| 平度市| 永善县| 宣威市| 开封县| 淅川县| 梁山县| 马公市| 扶沟县| 丹棱县| 富锦市| 宁南县| 北辰区| 时尚| 江油市| 曲周县| 阜新| 淮安市| 景德镇市| 安庆市| 文昌市| 贵定县| 花莲市| 囊谦县| 东宁县| 保德县| 福建省| 静乐县| 泽库县| 喜德县| 邳州市| 天镇县| 荃湾区| 云南省| 邯郸市| 建阳市| 泽州县| 天等县| 徐汇区| 靖远县|