current position:Home>JavaScript object

JavaScript object

2022-04-29 09:28:36Delete your code in the middle of the night·

JavaScript Object definitions

stay JavaScript in , almost “ Everything ” Are all objects .

  • Booleans are objects ( If you use new Keyword definition )

  • Numbers are objects ( If you use new Keyword definition )

  • Strings are objects ( If you use new Keyword definition )

  • Dates are always objects

  • Regular expressions are always objects

  • Arrays are always objects

  • Functions are always objects

  • Objects are always objects

all JavaScript value , In addition to the original value , Are all objects .

Create objects  

1. Use key words new Create objects

var obj = new Object();
obj.name = ' eldest brother ';
obj.age = 34;
obj.gender = true;
obj.eat = function(){
    console.log(' Eat stewed ~~');
    }
console.log(obj.name);
console.log(obj);
obj.eat(); 

2. Use factory classes to create objects

 function createPerson(name,age,gender){
                var obj = new Object();
                obj.name = name;
                obj.age = age;
                obj.gender = gender;
                obj.eat = function(){
                    console.log(obj.name+' Eat stewed ~~');
                }
                return obj;
            }
            
var person = createPerson(' eldest brother ',34,true);
var person = createPerson(' Eldest sister-in-law ',80,false);

3. Use construction methods to create objects

 

 

copyright notice
author[Delete your code in the middle of the night·],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204290928329027.html

Random recommended