JS Reference type---Object

Source: Internet
Author: User

First, the basic knowledge of the object
    • Concept: A particular thing, having characteristics (attributes) and behaviors (methods) that can store data.

    • Popular understanding: An object is a set of unordered key-value pairs that can store data.

    • Object's properties and methods: Can be . added in a way (this proves that JS is a dynamic type of language)

    • The value of most reference types in JS, all Object instances of type

Two, the object creation 3 ways 1.{}creating objects by literal volume
<script>    var={};    obj2.name=‘zhangxin‘;    console.log(obj2);</script>
2.newConstructors Create objects
    • Constructor: First letter uppercase (Convention); Normal function: First letter lowercase
<script>    var=newObject();    obj1.name=‘zhangxin‘;    console.log(obj1);</script>
    • newConstructor, new do 4 things:
      • Create a new object

      • thispoint to the new object that is currently created

      • Add to New Object属性、方法

      • Return创建后的新对象

<Script>    function Computer(Name,Age{         This.name =Name;         This. Age =Age;         This.say = function(){            return ' I am ' +  This.name + '; this year ' +  This. Age;        }    }    varCp= New Computer(' computer ',  -);    Console.Log(CP.say());  //I am a computer; this year</script>
    • new 构造函数Creating an Array Object
<script>    var arr = new Array();    console.log(arr);  // [] </script>
3.Object.create({键值对})method to create a new object; ES5 method, IE8 and the following incompatible
<script>    var o1 = {name: ‘zhangxin‘};    var create = Object.create(o1);    console.log(create.name);  // ‘zhangxin‘</script>
Third, the object's properties, methods (Assignment, take value) mode 1:.The way to add/read: Properties, method Mode 2:[]The way to add/read: Properties, methods(important)
    • Important: You can add dynamic property names, dynamic method names

    • Use [] the Add property, method: If the property name is fixed, quotation marks are required, otherwise it is considered a dynamic variable

<script>    var obj = {};        obj.age = 20;    obj[‘name‘] = ‘zhangxin‘;        console.log(obj.age);       // 20    console.log(obj[‘name‘]);   // ‘zhangxin‘</script>
Four, the object loop traversalfor-in
<script>    var={age:18,name:‘ss‘};        for (varin{        consloe.log(key);        console.log(obj[key]);    }</script>
V. Deep copy of ObjectObject.create( )
<script>    var={name:‘zhangxin‘};    var=Object.create(o1);    console.log=== o1);  // false</script>

JS Reference type---Object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.