JavaScript object, wrapper class

object

To add, delete, and modify the properties of an object:

Add: mrdeng.wire = "xiaoliu" write directly. . property =
Delete: delete mrDeng.name will delete the name directly
Change: mrdeng.wire = "xiaoliu", change the attributes in double quotation marks
Query: console.lgo (mrDeng. Attribute);

Object creation method:

1. Var obj = {} planobject object literal amount / object direct amount
2. Constructor
1) System built-in constructor new Object()
2) customization
3. Big hump operation rules
TheFirstName

Constructor internals

– implicit three-step transformation with new
1. Implicitly add this = {} at the front of the function body
2. Execute this.XXX = XXX
3. Implicitly return this

The instance of the constructor can not only use its own properties, but also use the properties of prototype. One of the properties in prototype is constructor, which points to the prototype of the constructor and records in the prototype which constructor the prototype is.

Example:

<script type = "text/javascript"> 
function Student(name,age,sex){
    //var this = {
    //   name : ""
    //   age : ""
    //  };
      this.name = name;
      this.age = age;
      this.grade = 2017;
    //return this;
}
var student = new Student('zhang',18,'male');

</script>

Packaging class

Basic packaging type (original value):

  • Number
  • String
  • Boolean

Property: the original value has no property and method, and cannot be assigned property value

Example:

<script type = "text/javascript"> 
    var num = 4;
    num.len = 3;  
    console.log(num.len);  
</script>
//undefined

Analysis:

<script type = "text/javascript"> 

    //Packaging class    
    var num = 4;

    num.len = 3;  //First of all, it is illegal to assign value to the original value in this sentence, so we use the wrapper class to solve this problem

    //new Number(4).len = 3;  delete  

    //The system automatically generates a newNumber(4) (now it is not the original value, so it can be assigned) and assigns the attribute value len to it. After the above steps are completed, it will be destroyed.

    //new Number(4).len; the new here is not the same as the above. The above has been destroyed, so there is no len attribute here
    console.log(num.len);  //When we visit here, the system finds that we are accessing invalid values again (it is still a legacy chain problem of assigning original values), so we make another correction (write it on this sentence)

</script>

//undefined

Example:

<script type = "text/javascript"> 
    var str = "abcd";
    str.length = 2;
    console.log(str);
</script>

Analysis:

<script type = "text/javascript"> 
    var str = "abcd";
    str.length = 2;     //Assign the original value, so use the wrapper class
    //new String('abcd').length = 2; delete

    //new String('abcd').length;    //new String Yes.lengthProperty oflengthAttribute value=4
    console.log(str.length);        //Note: This is different from the previous“.length"It is the system's own attribute, while the previous len Not attribute

</script>
//4

Example:

<script type = "text/javascript"> 
    var str = "abc";
    str += 1;
    var test = typeof(str);
    if(test.length == 6) {
        test.sign = "typeof The return result of may be string";
   }
    console.log(test.sign);
</script>

Analysis:

<script type = "text/javascript"> 
    var str = "abc";
    str += 1;
    var test = typeof(str);  //test = "string"
    if(test.length == 6) {
        test.sign = "typeof The return result of may be string";   //Because there is test = "string" in front of it, it is a string type now
        //new String(test).sign = 'xxx';     
   }
   //new String(test).sign 
    console.log(test.sign);
</script>

//undefined

Example:

<script type = "text/javascript"> 
    var x = 1;
    if(function f(){}) {
        x += typeof f;
   }
    console.log(x);
</script>

Analysis:

<script type = "text/javascript"> 
    var x = 1;
    if(function f(){}) {    // Note: function is parenthesized, so it becomes an expression,
                            //      So it can be executed immediately, and it will not be found after execution
        x += typeof f;      //  f is not defined,
                            //Generally, the system will report an error, but only typeof will return the string "undefined"
   }
    console.log(x);
</script>
//  1undefined

Example:

<script type = "text/javascript"> 
    function Person(name,age,sex) {   
        var a = 0;
        this.name = name;
        this.age = age;
        this.sex = sex;
        function sss () {
            a++;
            console.log(a);
        }
        this.say = sss;
    }
    var oPerson = new Person();
    oPerson.say();
    oPerson.say();
    var oPerson1 = new Person();
    oPerson1.say();
</script>
//1 2 1

Example:
Find the values of x, y, z.

<script type = "text/javascript"> 
    var x = 1, y = z = 0;
    function add(n)return n = n + 1;
    }
    y = add(x);
    function add(n) {
        return n = n + 3;
    }
    z = add(x);
</script>
//1 4 4

Analysis:

<script type = "text/javascript"> 
    var x = 1, y = z = 0;
    y = add(x);
    function add(n) {
        return n = n + 3;
    }
    z = add(x);
</script>
//1 4 4

The functions are the same, and the latter function overrides the former.

Exercise:
Calculate the length of the string, one alphanumeric and two Chinese characters.
Solution 1:

<script type = "text/javascript"> 
    var count = str.length;
    for(var i = 0; i < str.length; i++){
        if(str.charCodeAt(i) > 255){
            count ++;
        }
    }
</script>

Solution two:

<script type = "text/javascript"> 
    var code = 0;
    for(var i = 0 ; i<str.length; i++){
        if(str.charCodeAt(i) > 255) {
            count +=2;
        }else{
            count ++;
          }
    }
</script>

Keywords: Javascript Attribute

Added by Grimloch on Fri, 31 Jan 2020 22:38:22 +0200