Javascript: constructor or letter designation?

This question is more aimed at developers who do it professionally, and / or work as freelancers / in teams / for enterprises, etc.

Uses javascript literal notation more popular than constructor notation? Does it really matter which notation you use when writing Javascript? Are employers looking after, or is there a more professional notation?

LITERARY NOTE

var snoopy = {
    species: "beagle",
    age: 10
};

DESIGN NOTES

var buddy = new Object();
buddy.species = "golden retriever";
buddy.age = 5;
+4
source share
3 answers

, , , . , , , , .

, - , , .

var snoopy = {
    species: "beagle",
    age: 10
};

snoopy.peopleAge = convertDogAgeToPeopleAge(snoopy.age);

, " ", , , , . , , :

function Animal(species, age) {
    this.species = species;
    this.age = age;
}

var buddy = new Animal("golden retriever", 5);
console.log(buddy.species);    // "golden retriever"

var snoopy = new Animal("beagle", 10);
console.log(snoopy.species);    // "beagle"

, , - . :

var x = {};
var y = new Object();

, , , , , Javascript, , , {} [] , new xxxx().

+1

new Object() {} , . :

let obj = {
    prop: 'val',
    .
    .
}

-, , : obj. .

. new Array() [] , , new Array(<length>) , . , , undefined, map(), reduce(), forEach , "" -, .

- , "" Javascript, :

function MyType () {
    this.prop = '';
}

:

var type = new MyType();

type instanceof MyType, .

0

, :

  • . , . .

  • JS- . ​​, .

  • JSON. , JS, JSON, , .

  • JS minifiers var foo = new Object(); var f=new Object; var f={};

  • Google says to use literals. I may be biased, but listening to them sounds like a good plan.

  • As shown in the Google Style Guide, accessing properties does not work with the dot operator if the property name has special characters, so accessing properties looks less consistent. Meanwhile, special property names must be specified to work in literals.

0
source

Source: https://habr.com/ru/post/1652792/


All Articles