>  기사  >  웹 프론트엔드  >  JS 클론, 속성, 배열, 객체, 함수 인스턴스 분석

JS 클론, 속성, 배열, 객체, 함수 인스턴스 분석

高洛峰
高洛峰원래의
2016-12-05 10:13:56950검색

이 문서의 예에서는 JS 복제, 속성, 배열, 개체 및 함수를 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

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

여기의 console.log는 매우 흥미롭습니다. Alert는 모든 데이터를 가져올 수 없으며 하나씩 팝업해야 합니다. 하나.

js의 배열 정의도 매우 흥미롭습니다.

추가 업그레이드

<script type="text/javascript">
/* 克隆原型得到对象 */
function clone(object) {
  function F() {}
  F.prototype = object;
  return new F;
}
var Person = {
 name: &#39;default name&#39;,
 getName: function() {
  return this.name;
 }
};
var Author = clone(Person);
Author.books = []; // 书数组
Author.getBooks = function() {
 return this.books;
}
var authorClone = clone(Author);
console.log(authorClone.name); // string &#39;default name&#39;.
authorClone.name = &#39;new name&#39;; // 重新赋值
console.log(authorClone.name); // Now linked to the primative authorClone.name, which
// is the string &#39;new name&#39;.
console.log(Author.getName()); // 没有改变,任然是 &#39;default name&#39;
console.log(Author.getBooks()); // 空的
authorClone.books.push(&#39;new book&#39;); // Author被改了
authorClone.books.push(&#39;new new book&#39;); // Author被改了
console.log(Author.getBooks()); // array &#39;new book&#39;
console.log(authorClone.getBooks()); // array &#39;new book&#39;
authorClone.books = []; // 定义了属于自己的books数组
authorClone.books.push(&#39;new book2&#39;); // We are now modifying that new array.
authorClone.books.push(&#39;new book3&#39;);
authorClone.books.push(&#39;new book4&#39;);
console.log(authorClone.getBooks());
console.log(Author.getBooks());
var CompoundObject = {
 string1: &#39;default value&#39;,
 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>


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.