Is this all an object?

In one popular blog, the author asked his listeners what their “Ah ha!” Was. moment for JavaScript, and most people said that it understood that everything in JavaScript is an object. But, being new to JS and programming in general, I don't quite understand what that means. This is not like being linked to a real JS object - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

Or that?

If you cannot explain what they mean, "everything in JavaScript is an object."

Or is it all about programming OO as a whole and reading something on this topic will help to understand this? Can you read what to read on this topic?

+1
source share
4 answers

Return to the first principles.

Which object? This is a software component that encapsulates state and behavior together into a single object in memory.

In this definition, you can see where everything can be seen as an object. Functional programmers perform the functions of objects of the first class. Data people say that data, even without behavior, can be thought of as an object (albeit not very smart).

I do not see that will change.

JavaScript treats functions as objects.

I'm not sure what effect this understanding will have in your programming.

+6
source

What they mean most likely is that any data that can be assigned to a variable has properties (and therefore methods) that can be accessed in an object similar to the method.

// strings "asdf".length; // 4 "asdf".replace('f', 'zxc'); // "azxc" // numbers (10).toFixed(2); // "10.00" // booleans true.someProp; // undefined (point is it doesn't crash) 

They even have prototypes that they inherit.

 "omg".constructor; // function String() { [native code] } String.prototype.snazzify = function() { return "*!*!*" + this + "*!*!*"; }; "omg".snazzify(); // "*!*!*omg*!*!*" 

However, these are primitives, and although they behave like an object, as in many ways, they are somewhat different from other "real" JS objects. The biggest of them is that they are immutable.

 var s = "qwerty"; s.foo; // undefined, but does not crash s.foo = 'some val'; // try to add a property to the string s.foo; // still undefined, you cannot modify a primitive 

Please note that these functions are real mutable objects.

 var fn = function(){}; fn.foo; // undefined fn.foo = 'some val'; // try to add a property to the function fn.foo; // "some val" 

So, although it’s technically not true that “everything in JS is an object”, in most cases you can treat them mostly as objects, because they have properties and methods and can potentially be extended. Just make sure you understand the disclaimers.

+7
source

' ALMOST everything is an object because MAIN codes are JS objects. In primitives you cannot add elements, for example, as for all objects. My answer is why JS functions are JS objects here: fooobar.com/questions/164142 / ...

0
source

NOT everything in JavaScript is an object.

All data in JavaScript must fall into one of six primitive types or object types. Primitive types include boolean, null , undefined , string, number, and character; everything that is not primitive is an object .

This means that functions are objects, arrays are objects, ES6 de-sugar classes into functions that are objects.

The confusion arises because primitive values ​​have wrappers on objects. When you try to access the length property in a string literal, JavaScript creates a temporary wrapper for the object around the primitive and accesses the length property of the wrapper for that object. After the property has been restored, the wrapper of the object is discarded. This is called auto-boxing.

Essentially, it is implemented similarly to the following:

 const foo = "bar"; // When you run `foo.length`, it similar to tmp = String(foo); tmp.length; delete tmp; 

or

 const foo = "bar"; (new String(foo)).length; 

String, number, and boolean primitives have object wrappers, but null and undefined do not. Therefore, an attempt to access a property or method from these primitives will cause an error.

 null.length; // Uncaught TypeError: Cannot read property 'length' of null undefined.length; // Uncaught TypeError: Cannot read property 'length' of undefined 
0
source

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


All Articles