Chapter V web front-end development engineer --JavaScript Advanced Programming 5-4 JavaScript object created

                                                                       JavaScript objects

 

This lesson is to talk:

  1. JavaScript object creation
  2. JavaScript Object basic operations
  3. JavaScript function object

                                                                 Speaker teachers: Head teacher

A. JavaScript object created

1. Types of objects

1.js like built-in objects such as Number

2. The host environment runs in a browser window document

3. The creation of the object itself

2. Creating objects

1. Create an object, then the object to the new properties and methods.

Box new new Object = var (); // create an Object Object

= box.name 'Lee'; // create and assign a name attribute

= 100 box.age; // create a property and assign age

= function box.run () { // create a run () method returns the value and

return this.name + this.age + 'operation ...';

};

alert (() box.run); value // output attributes and methods

The above creates an object, and create properties and methods, this in the run () method in, the box is to represent the object itself. This method is the most basic JavaScript object is created.

Define and modify the properties of the method

Object.defineProperty (Object, "Properties", "characteristics of the object property")

Object.defineProperty(p,“yy”,{value:100,writeable:false});

p.yy

p.yy=200;

p.yy

Add more properties:

object.defineProperties(p,{

abc:{

value:1000,

writeable:false

},

qwe{

value:true

},

height:{

get:function(){

return :180

},

Set:function(val){

Console.log(val);

}

}

})

 

3. Because of the way to create an object above can easily modify object properties, we use get set way to create.

 var p = {

        name:"Head",

        work:function() {

            console.log("wording...");

        },

        _age: 18, // if the property inside the object calling convention of naming is _age

        get age(){

            this._age return;       // GET is used to return

        },

        Age SET (Val) { // SET is used to set the

            if (val<0 || val> 150) {

                throw new Error ( "Age is not lawful.")

            }else{

                this._age = val;

            }

        }

    };

    console.log(p.name);

    console.log(p.age); console.log(p.name);//输出chen

A direct result of debugging browser:

    p.age

    "18"

    p.age=23

    "23"

    p.age=200

    Uncaught Error: invalid value

Guess you like

Origin blog.csdn.net/wgf5845201314/article/details/92380625