How to create custom objects:

Keywords: Javascript Java Attribute JSON

1. Factory mode

Advantages: The ability to create objects in batches.
Disadvantage: The method above is added for each object.

function createCat(name, age, color) {
            var cat = new Object();
            cat.name = name;
            cat.age = age;
            cat.color = color;
            cat.catchM = function () {
                return this.name + 'Catching mice';
            };
            return cat;
        }
        var cat1 = createCat('Mimi', 1, 'white');
        var cat2 = createCat('Joy and joy', 2, 'yellow');
       
        console.log(cat1.catchM()==cat2.catchM())//false
       
        console.log(cat1);

2. Constructor pattern:

Constructors need to be instantiated, and new keywords need to be used.

function dog(name, age, color) {
            this.name = name;
            this.age = age;
            this.color = color;
            this.run = function () {
                console.log( this.name + 'Running');
            }
        }
        var dog1 = new dog('xixi', 1, 'white');
        console.log(dog1);

      
  var dog2 = new dog('tt', 1, 'black');
        console.log(dog2);
        console.log(dog1.run());
        console.log(dog2.run() == dog1.run())//flase

The difference between factory pattern and constructor:
The constructor does not need a return statement.
The constructor does not explicitly create objects.
The constructor assigns attributes and methods directly to this object.

3. Prototype mode:

function book() {

        } 
        book.prototype.name = 'javascript';
        book.prototype.page = 600;
        book.prototype.look = function () {
            console.log(this.name)
        }
        var o1 = new book();
        o1.name='java';
        o1.page=200;
        console.log(o1);
        o1.look();

        var o2 = new book();
        o2.name='javaaaa'
        console.log(o2);
        o2.look();

console.log(o1.look == o2.look)//true


     
function book() {
        
        } 
        book.prototype.name = 'javascript';
        book.prototype.page = 600;
        book.prototype.fri=[1,2,3]      
        book.prototype.look = function () {
            console.log(this.name)
        }
        
        var o1 = new book();
        o1.name='java';
        o1.page=200;
        o1.fri=[1,2,3];

If this is o1.fri.push(4); then console. log (o1. fri = o2. fri) / true.
Because the changed fri attribute belongs to book.prototype.

    var o2 = new book();
    console.log(o1.fri == o2.fri)//flase

4. Mixed mode: Combining constructor mode and prototype mode

Put the attribute in the constructor.
Put the method into the prototype;
Each instance has its own attributes, and at the same time, it shares methods, which saves memory to the greatest extent, and also supports constructor parameter transfer.

  function book(name, page) {
            this.name = name;
            this.page = page;
        }
        book.prototype.look = function () {
            console.log(this.name + 'Read a Book')
        }
        var o1 = new book('jav', 200);
        o1.look();
        var o2 = new book('javaaa', 300);
        console.log(o2);
        o2.look();
     console.log(o1.look == o2.look);//true

5.json Format Create Objects

   var cup = {
            name: 'Blue and white',
            age: 2,
            drink: function () {
                console.log('heshui')
            }
        }
        cup.drink();
     console.log(cup.name);

Posted by MarkB423 on Tue, 08 Oct 2019 09:59:09 -0700