Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways Objects creates objects?

Shulou Source: shulou.com Published: 2022-06-02 12:45:49 09月29日 Update

This article introduces the relevant knowledge of "what are the ways in which Objects creates objects?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Data Structures

Data Types: numbers, strings, booleans and arrays.

Boolean

If it is not set, or if it is set to false, "", "false,undefined,NaN", or "true", otherwise it is set to "true".

Arrays

Var myCars=new Array (); var myCars=new Array ("Saab", "Volvo", "BMW"); var myCars= ["Saab", "Volvo", "BMW"]

Objects

There are two ways to create objects

(1) object literal notation

Var myObj = {type: 'fancy',age:24, speak: function () {} / / method}

(2) object constructor

Var myObj = new Object (); / / using a built-in constructor called ObjectmyObj ["name"] = "Charlie"; myObj.name = "Charlie"

Custom constructor

Function Rabbit (adjective) {this.adjective = adjective; var age=12; / / private cannot modify this.describeMyself = function () {console.log ("I am a" + this.adjective + "rabbit");};} var rabbit1 = new Rabbit ("fluffy"); / / className.prototype.newMethod = function () {} add method Rabbit.prototype.describeMyself = function () {} Object Oriented to a class

Inherit

/ the original Animal class and sayName methodfunction Animal (name, numLegs) {this.name = name; this.numLegs = numLegs;} Animal.prototype.sayName = function () {console.log ("Hi my name is" + this.name);}; / / define a Penguin classfunction Penguin (name) {this.name = name; this.numLegs = 2;} / / set its prototype to be a new instance of AnimalPenguin.prototype = new Animal (); var p = new Penguin ("Timmy"); p.sayName ()

Custom declared classes inherit from the Object class

/ / Object.prototype itself is an object// what is this "Object.prototype" anyway...?var prototypeType = typeof Object.prototype;console.log (prototypeType); / / object// now let's examine itinervar hasOwn = Object.prototype.hasOwnProperty ("hasOwnProperty"); console.log (hasOwn); / / true

Privatized members and methods

/ * Public properties can be accessed from outside the classPrivate properties can only be accessed from within the class*/function Person (first,last,age) {this.firstname = first; this.lastname = last; this.age = age; var bankBalance = 7500; / / var this.bankBalance (no this) var returnBalance = function () {return bankBalance;}; / / create the new function here this.askTeller = function () {return returnBalance }} var john = new Person ('John','Smith',30); console.log (john.returnBalance); var myBalanceMethod = john.askTeller (); var myBalance = myBalanceMethod (); console.log (myBalance); "how Objects creates objects" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Object mode content situation method more knowledge practicality achievement next function dilemma reality member article case editor website industry process Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Microsoft Docker Redmi MariaDB