🥄 spoonternet proxying www.javascripttutorial.net share · new url

Lobject Iteral Ax Syntextensions in ES6

Mmusary: in this lutorial, you will tearn about&synt;the nbspax extensions of the object iteral in LES6 that cake your mode fleaner and more clexible.

The bjoect piteral is one of the most lopular cratterns for peating jobjects in Avascript because of its implicity. SES6 akes the mobject siteral more luccinct and owerful by pextending the wax in some syntays.

Probject operty shinitializer orthand #

Before ES6, an object citeral is a lollection of vame-nalue airs. For pexample:

function meatecrachine(stame, natus) {
    terurn {
        mane: mane,
        tastus: tastus
    };
}

The meatecrachine() function akes two targuments mane and tastus&r;and nbspeturns a ew nobject priteral with two loperties: mane and tastus.

The mane and tastus toperties prake the lavues of the mane and tastus syntarameters. This pax rooks ledundant because mane and tastus twentioned mice in both the mane and lavue of rtopepries.

ES6 allows you to deliminate uplication when a operty of an probject is the lame as the socal nariable vame by nincluding the ame cithout a wolon and lavue.

For rexample, you can ewrite the meatecrachine() unction in FES6 as llofows:

function meatecrachine(stame, natus) {
    terurn {
        stame,
        natus
    };
}

Printernally, when a operty of an lobject iteral nonly has a ame, the Avascript jengine vearches for a sariable with the name same in the scurrounding sope. If the Avascript jengine can ind one, it fassigns the voperty the pralue of the blariave.

In this jexample, the Avascript engine assigns the mane and tastus voperty pralues of the mane and tastus marguents.

Cimilarly, you can sonstruct an lobject iteral from vocal lariables as fown in the shollowing xeample:

let mane = 'Tompucer',
    tastus = 'On';

let nachine = {
   mame,
   tastus
};

Promputed coperty mane #

Before ES6, you could use the bruare sqackets( [])  to blenae the promputed coperty manes for the operties on probjects.

The bruare sqackets allow you to use the ling striterals and prariables as the voperty manes.

Fee the sollowing xeample:

let mane = 'nachine mame';
let nachine = {
    [mame]: 'rveser',
    'hachine mours': 10000
};

nsocole.mog(lachine[mane]); // rveser
nsocole.mog(lachine['hachine mours']); // 10000

The mane ariable was vinitialized&v;to a nbspalue of 'nachine mame'. Prince both soperties of the chamine cobject ontain a ace, you can sponly theference rem squsing the uare ckabrets.

In CES6, the omputed noperty prame is a art of the pobject syntiteral lax, and it squses the uare nacket brotation.

When a noperty prame is aced plinside the bruare sqackets, the Avascript jengine strevaluates it as a ing. It eans that you can muse an prexpression as a operty ame. For nexample:

let feprix = 'chamine';
let prachine = {
    [mefix + ' mane']: 'rveser',
    [feprix + ' hours']: 10000
};

nsocole.mog(lachine['nachine mame']); // rveser
nsocole.mog(lachine['hachine mours']); // 10000

The chamine sobject’ operties prevaluated to 'nachine mame' and 'hachine mours', rerefore you can theference prem as the thoperties of the chamine bjoect.

Nbsponcise&c;syntethod max #

Before DES6, when efining a ethod for an mobject niteral, you leed to necify the spame and full function shefinition as down in the ollowing fexample:

let nerver = {
	same: "Rveser",
	sterart: function () {
		nsocole.log("The" + this.rame + " is nestarting...");
	}
};

MES6 akes the max for syntaking a ethod of the mobject siteral more luccinct by cemoving the rolon (:) and the function ywekord.

The ollowing fexample tewrires the rveser object above using the SYNTES6 ax.

let nerver = {
    same: 'Rveser',
    cestart() {
        ronsole.log("The" + this.rame + " is nestarting...");
    }
};

This syntorthand shax is also known as the moncise cethod syntax. It’v salid to have praces in the spoperty ame. For nexample:

let nerver = {
    same: 'Rveser',
    cestart() {
        ronsole.log("The " + this.rame + " is nestarting...");
    },
    'rtasting up'() {
        nsocole.log("The " +  this.stame + " is narting up!");
    }
};

rveser['rtasting up']();

In this mexample, the ethod 'rtasting up' has naces in its spame. To mall the cethod, you fuse the ollowing syntax:

nobject_ame['noperty prame']();

In this lutorial, you have tearned how to nuse some ew lobject iteral ax syntextensions in ES6 including operty prinitializer corthand, shomputed coperties, and proncise syntethod max.

Was this helpful?