0%

JavaScript Object

  • Create object
  • Factory funnction
  • Construtor function
  • Operate object properties and methods
  • Enumerating object
  • Clone object
  • Garbage collector
  • Template literals
  • Math, String, Date
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    // object-oriented programming (OOP)

    const circle0 = {
    radius: 1,
    location: {
    x: 1,
    y: 1
    },
    isVisible: true,
    draw: function(){
    console.log('draw');
    }
    };
    circle0.draw();

    // factory functions: create objects. Camel notation
    // function createCircle(radius){
    // const circle = {
    // radius: radius,
    // draw: function() {
    // console.log('draw1');
    // }
    // };
    // return circle;
    // }

    function createCircle(radius){
    return {
    radius,
    draw(){
    console.log('draw2')
    }
    };
    }
    const circle1 = createCircle(1);
    console.log(circle1);


    // constructor function. Pascal Notation.

    function Circle(radius){
    this.radius = radius // this: represents an empty object
    this.draw = function() {
    console.log('draw');
    }
    }
    const circle2 = new Circle(1);

    // enumerating properties of an object
    console.log("=====enumerating of object");
    for (let key in circle2){
    console.log(key,circle2[key]);
    }

    for (let key of Object.keys(circle2)){
    console.log(key);
    }

    for (let entry of Object.entries(circle2)){
    console.log(entry);
    }

    // clone object
    console.log("====clone oject");
    /* const another = {};
    for (let key in circle2){
    another[key] = circle2[key];
    } */

    /* const another = Object.assign({},circle2); */
    const another = {...circle2};
    console.log(another)

    // object

    console.log("----add/delete properties and methods")
    const circle3 = {
    radius: 1,
    location: {
    x: 1,
    y: 1
    },
    isVisible: true,
    draw: function(){
    console.log('draw');
    }
    };
    circle3.color = 'yellow';
    circle3.move = function(){};
    console.log(circle3);

    delete circle3.color;
    delete circle3.move;
    console.log(circle3);

    // constructor property: every object has a constructor property.
    //that references the function that was used to create the object.

    // in js, functions are objects

    // gabarge collecter: automatically

    // math
    // string: two types

    // template literals 模板语法
    console.log("----template literals");
    const name = "Chandler";
    const anotherMessage =
    `Hello ${name} ${3+3},

    Thanks for your book 'Little Woman', I like it so much!

    Best,
    Monica
    `
    console.log(anotherMessage);

    // date
    console.log('----date');
    const now = new Date();
    console.log(now);
    console.log(now.toDateString());
    console.log(now.toTimeString());

Thanks for @Mosh‘s great video tutorials.

-------------End of blogThanks for your reading-------------