javaScript: can someone explain this - javascript

What does this mean?
var settings = {
"column-1" : ["block-1"],
"column-2" : ["block-2"]
};

It means one should [likely] read a tutorial/book on JavaScript before asking questions like this on SO ;-) (Explaining what "it means" will likely have not much "practical meaning" by itself.)
Like Eloquent JavaScript: A Modern Introduction to Programming
I believe the exact construct/term that is being sought is "object literals" -- {...} is for objects and [...] is for arrays.
Happy coding.

This will create a new object and store it in the settings variable.
The object is created by an Object literal and consist out of two propertys (column-1 and column-2) which are both assigned an Array with a single String value.

It defines an object containing two properties (column-1 and column-2) which both contain arrays which both contain a single value (block-1 an block-2).
Due to the - in the property name it will be impossible to access them using the object.property syntax so you'll have to use the array syntax: object['property']

Initialized a variable named settings and assigned the value {"column-1": ["block"], "column-2": ["block-2"]}, which is an object, to the settings.

Related

Is a variable an Object in JS

I'm currently learning JS, and while working on my project, I was wondering if a variable is by definition an object, or a kind of object, or nothing a all.
I know we can create objects through var, but I'm not sure if a var is always an object.
Thanks for the answers !
Variables are not classified as objects. They are their own classification as a storage address. Now as Nina said in the comment, objects are types which are in the same category as: array, string, number, and boolean. A variable can hold any of these types and be referenced to in your code.
A variable is rather like a bucket, where different values can be put in. An object is such a value.
I know we can create objects through var
Not really. With the var keyword you can declare a variable (a bucket), to create an object you could use an object literal.
Think of var as a mechanism to create a memorable handle for your primitive and reference values like your object. The var keyword itself doesn't do anything other than declare a handle in a scope and allow you to initialize the handle to the value you desire.

JavaScript Array behaving like an object

I am trying to understand something in JavaScript.
Lets say I do the following:
let x = [];
x['Monday'] = 'Work';
x['Tuesday'] = 'More Work';
console.log(x) //[Monday: "Work", Tuesday: "More Work"]
console.log(x.Monday) //"Work"
console.log(x.Tuesday) //"More Work"
Can someone help explain why the array now behaves like an Object?
Because array IS an Object
[] instanceof Object
> true
[] instanceof Array
> true
Both give you true because Array extends normal Object functionality. Furthermore, the array indexes all its members by string values
const array = [];
array[0] = 1;
array['0'] // will give you 1
which is also unexpected by most of the people. So, the array behaves like an Object even if you use it as a normal array with indices
Everything in JS is an Object except the Primitive types (and even those come with Object wrappers)
That is why you can access properties and methods like array.length array.indexOf() etc just like you would with any other object. The indices are like special properties on the array object which are iterable
over all JavaScript array is also an object
if you check the type of x
typeof x
it will prints "object"
var x =[];
x.name = "X";
here we are creating a property "name" of x array
Because in javascript there is no real Array type as a low level type. Array class is just a kind of a class which extended from Object class.
Every class in javascript except primitives like Number, String ect. are extended from Object class.
Every object (instance of class) can have properties and methods.
Every instance of classes can have new properties and methods on the run-time as well as this properties could be changed modified or deleted.
So by doing this.
var ar = [] // I used array literal. Not totally same but similar of doing new Array();
arr['foo'] = 'bar';
You are just adding a new property to an object.
I think you’ve heard by now that Array is a type of Object, so I won’t beat that dead horse any further. Well maybe a little.
Which means you can accsss and set properties with names like “Monday” and “Tuesday”. Since it’s a type of Object this is no problem (and a statement like a[‘1’] is implicitly converting that string ‘1’ to a number 1). But don’t mistake this property name to be an array index, those are specifically integers**. What’s the importance of that?
Arrays have a nice behaviour that when you use an actual array index, the length property is automatically updated for you. Not to mention array index access is usually optimized by JVM implementations to be noticeably faster than regular property accesses.
Also let’s not forget one of the other huge benefits of having Arrays, they inherit from the Array.prototype, which has a whole lot of useful methods :)!
So in short use Arrays like Arrays — gain the perf boost while also making sure the code is easier to comprehend.
Source: JS Definitive Guide 6th - Array chapter
(** non-negative and less than 2^32-2 since arrays has 32-bit size indexes.)

Access data in javascript object

I ran console.log(object). This is what is shown in the console:
How can I access any of this information? For example the info in $oel
Like many languages, JavaScript syntax uses "dot notation" to access properties on objects:
object.$oel
Those properties may be primitive values:
console.log(object.$oel)
or perhaps objects with their own properties:
console.log(object.$oel.someProperty)
or perhaps functions:
object.$oel()
Regardless of the type, the syntax to reach it is the same.
You can you standard dot notation to access object attributes, eg:
object.attr;
Regards.

What is the name for adding properties and functions to an object at runtime in JavaScript?

In JavaScript it is possible to add both properties and functions to objects (and functions) dynamically? What is the name for this?
var obj = {};
obj.myProperty = "I have just beed added!";
In Javascript, everything is always made at runtime.
So I'd call it "adding properties and functions to an object".
Just to contribute to the sum of knowledge, as I now feel bad about my comment re homework...
This is known simply as property assignment because you are assigning a value to a property.
EDIT:
For the record, section 3 of chapter 3: Objects, in JavaScript the good parts by Douglas Crockford, states thus:
A value in an object can be updated by assignment. If the property
name already exists in the object, the property value is replaced
(emphasis added)
JavaScript is prototype based language so you can call it as prototype behavior.
Augmenting Types: Crockford Page 32, 'JavaScript the Good Parts'. SO I guess we could call this: augmentation. He goes on to talk about augmenting the prototype but in the opening paragraph he says:
"JavaScript allows the basic types of the language to be augmented. In chapter 3, we saw that adding a method to Object.prototype makes that method available to all objects. This also works for functions, arrays strings, numbers, regular expressions and booleans."
He then goes on to give examples of augmenting the prototype. But, as the sentence above shows augmenting can be applied to all JavaScript basic types.
So I guess augmenting is a fair enough word.

Javascript expando objects

What are expando objects in javascripts?
For what purpose we need this ? Any complete example will be appreciated
I found 1 article here Javascript: The red-headed stepchild of web development
Well, in javascript, any object is an expando object. What it means is, as the article covers, that whenever you try to access a property1 it will automatically be created.
var myObj = {}; // completely empty object
myObj.myProp = 'value';
The moment you assign myProp a value, the property myProp is dynamically created, eventhough it didn't exist before. In a lot of other languages, such as C#, this is not normally possible (actually C# has just enabled expando object support as well, but that's besides the point). To access a property in a normal class in C#, you need to specify in the class that it does indeed have this property.
1 Not quite correct. See npup's comment below for clarification.
Everything except primitive types(string, number,boolean) are objects and support Key:values structure. properties(keys) can be accessed and set using the dot notation as well as the square brackets.
var myObj = {};
myObj.myProp1 = 'value1'; //works, an expando property
myObj[myProp2] = 'value2'; // doesn't work, myProp2 is an undefined name.
myObj['myProp2'] = 'value2'; // works , an expando property
myObj[2010]= 'value'; //note the key is number, still works, an expando property??
myObj.2010 = 'value'; // FAILS. to use dot notation, key must be a string
An article written in 2007 that uses document.all (as the only way to access elements)? That's a big red flag.
It is just dressing up "You can add properties to an object" with some buzzwords.
We need to be able to do this because otherwise we wouldn't be able to store data, and that would make JavaScript a pretty useless language.
(Everything is an array? No it isn't. And it iterates over an object without a hasOwnProperty wrapper. That isn't safe. Just keep away from the article, it is worse than useless)
JavaScript turns elements with specific IDs of names into expandos of the returned DOM object. It is explained here.

Categories

Resources