Everything is an object?

前端 未结 5 1469
南笙
南笙 2020-12-18 14:49

At one popular blog, the author asked his audience what was their “Ah ha!” moment for JavaScript and most of the people said that it was realizing that everything in JavaScr

相关标签:
5条回答
  • 2020-12-18 15:03

    Go back to first principles.

    What's an object? It's a software component that encapsulates state and behavior together into a single entity in memory.

    By that definition, you can see where everything can be thought of as an object. Functional programmers make functions first class objects. Data folks would say that data, even that without behavior, can be thought of as an object (albeit not a very smart one).

    I don't see what this changes.

    JavaScript treats functions as objects.

    I'm not sure what effect this insight will have on your programming.

    0 讨论(0)
  • 2020-12-18 15:12

    Not everything is an object in JavaScript. For example a number is not an object.

    Different languages have different definition of "object". For JavaScript, the official definitions can be found in the ECMAScript Language Specification, which states:

    In ECMAScript, an object is a collection of zero or more properties each with attributes that determine how each property can be used (...) Properties are containers that hold other objects, primitive values, or functions. A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, BigInt, String, and Symbol; an object is a member of the built-in type Object; and a function is a callable object.

    So by this definition it is clear that primitive values (like numbers) are not objects. Even strings are not objects, which is different from most other OO languages.

    So why do some people say "everything is an object"? You have to ask them! But I suspect it is because they are getting confused by the built-in objects Number, Boolean, String etc. which can be used as wrappers around the corresponding primitive values. This wrapping sometimes happen automatically, which can make primitive values look like object and for example allow you to (seemingly) access properties on primitives. But in reality the properties are on the wrapper object.

    0 讨论(0)
  • 2020-12-18 15:21

    NOT everything in JavaScript is an object.

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

    This means functions are objects, arrays are objects, ES6 classes de-sugars into a function which are objects.

    The confusion arises because primitive values have object wrappers. When you try to access the length property on the string literal, JavaScript creates a temporary object wrapper around the primitive and access the length property of that object wrapper. After the property has been retrieved, the object wrapper is discarded. This is known as autoboxing.

    Essentially, it is implemented similar to the following:

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

    Or

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

    The string, number and boolean primitives have object wrappers, but null and undefined do not. So trying to access a property or method from those primitives would throw an error.

    null.length; // Uncaught TypeError: Cannot read property 'length' of null
    undefined.length; // Uncaught TypeError: Cannot read property 'length' of undefined
    
    0 讨论(0)
  • 2020-12-18 15:21

    'ALMOST everything is an object' because the MAIN code-units are JS-objects. On primitives you can NOT add members for example as on all objects. My answer why JS-functions are JS-objects here: https://stackoverflow.com/a/24811539

    0 讨论(0)
  • 2020-12-18 15:29

    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 like fashion.

    // 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 they inherit from.

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

    However, these are primitives, and while they behave object like in a lot of ways, they are different from other "real" JS objects in a few ways. The biggest of which 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
    

    Do note though that 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 while it's not technically true that "everything in JS is an object", under most circumstances you can treat them mostly like objects in that they have properties and methods, and can potentially be extended. Just be sure you understand the caveats.

    0 讨论(0)
提交回复
热议问题