Colon vs Equal


#1
class Surgeon {
  
  constructor(name, department) {
    
   this._name = name;
   this._department = department;
  
  }

}

Why can I not list the values of my keys with colons?

method(key) {

this._keyName: 'name'

}

I was under the impression that = was not used to define key values. Come to think of it, I’ve been doing it all along in these tutorials but now I’m stuck understanding why and when to use a colon.

I understand it’s an ‘object literal’ that helps to list key values, so why is it not used here? Is this specific syntax to the constructor method?

Is it specific to the this. function call?

I’m going to read some manual entries meanwhile…


#2

i think you are confusing two things, when creating objects we use : for key value pairs:

let example = {
    key: 'value';
}

= for just assigning/defining variable, we define this._name and this._department variables in a method.

Javascript is a confusing language. There is also a difference between objects and classes (classes where introduced in es6, which is what you are currently using given the class keyword)


#3

Yes, the semantics are what’s hardest to grasp!

Would these statements be correct?:

I create an object, and assign/define a variable inside with :
I create a class, and assign/define a variable with =


#4

its important to understand what es6 means, es6 (released in 2015) made large changes to the Javascript language. New things where added, like the class keyword (allowing classes) while before (es5) we only had objects, classes didn’t exist yet. But es6 also made changes to already existing things like shorter notation of methods:

// es5 
var obj = {
  foo: function() {
    /* code */
  },
  bar: function() {
    /* code */
  }
};

// es6
var obj = {
  foo() {
    /* code */
  },
  bar() {
    /* code */
  }
};

this is not entirely true, properties of an object (like the methods in the example above) use the colon, however, within the methods itself, you could again define variable using an equal sign.

also not entirely true, inside the class, you could create an object (with equal sign) then the properties of the object would use colons.

Javascript is a difficult first language, given how many quirks the language has.


#5

Thank you so much for looking after my query.

I really appreciate the effort, I understand it a bit better now. I’m gearing myself to change careers so having this kind of forum resource is invaluable.

Things are a bit clearer now.


#6

if you really want to get in-depth with JS, i recommend this book:

http://shop.oreilly.com/product/9780596517748.do

i am reading you don’t know javascript from the same author, its really well written. If you have some money to spare, i can highly recommend it.

The book also seems downloadable, but not sure that is the intention.

Anyway, i hoped my answers helped a bit


#7

Will do, the description must describe so much of the programming world…

JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined.


#8

Refining is expensive, and if you have a deadline you might cut a few corners to meet the deadline. But then you have your product, so making changes becomes difficult. Its a vicious cycle