1216 After-school essay

1. Object:

    Object contains many values: each value by Name: Composition attribute value;

each person = {
    name: 'bill',
    sex: 'male'
}

    Getting a property value way: person [ 'name']; person.name;

    After a good object definition can be modified: person.name = "joney";

    You can continue to add value: person.height = 180;

    Delete Attribute: delete person.height;

    Object may comprise a function; known method

each person = {
    firstnam:’bill’,
    speak:function(){
        return ‘my name is’+this.firstnam;
    }
}

    this keyword: this refers to the person on the object;

    Performing object methods: person.speak ();

    Using the new way to define an object;

    var person = new object();

    person.firtname=”bill”;

    person.age = 50;

    Constructor function: to create a single object above; something we need to create many objects of the same type, you need the object constructor;

    With the initials of the constructor function name is a good idea.

    A new method or a new property constructor added, must be added to the constructor function;

    prototype property:

    Sometimes we want to add new properties or methods to an object constructor;

    Soldier.prototype.lxl = 100;

 

    Exercise:

function Soldier(name,dagger=10){
    this.name = name;
    this.dagger = dagger;
    this.xl = 10;
    this.gj = function(){
        this.dagger--;
        return '剩余匕首'+this.dagger;
    }
    the this .fy =  function () {
         var  I = the parseInt (Math.random () * 2 );
         the this .xl - =  I;
         return  'Health remaining' + the this .xl;
    }
}
was  zs =  new  Soldier ( 'zhangsan', 12 );
was  ls =  new  Soldier ( 'Lisi');

Guess you like

Origin www.cnblogs.com/ltl11230/p/12076462.html