Hàm get trong Javascript
Trong bài này chúng ta sẽ tìm hiểu hàm Array.find() trong Javascript, hàm find sẽ trả về giá trị của phần tử đầu tiên trong mảng thỏa mãn được điều kiện kiểm tra (được truyền vào như một hàm). Show
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức. Hàm find sẽ lần lượt sử dụng các phần tử của mảng để thực hiện hàm kiểm tra cho đến khi có giá trị thỏa mãn và hàm kiểm tra trả về true. Khi đó hàm find sẽ trả về giá trị của phần tử thỏa mãn và bỏ qua không kiểm tra các phần tử còn lại. The 0 syntax binds an object property to a function that will be called when that property is looked up.Try itSyntax
Parameters 1The name of the property to bind to the given function. 2You can also use expressions for a computed property name to bind to the given function. DescriptionSometimes it is desirable to allow access to a property that returns a dynamically computed value, or you may want to reflect the status of an internal variable without requiring the use of explicit method calls. In JavaScript, this can be accomplished with the use of a getter. It is not possible to simultaneously have a getter bound to a property and have that property actually hold a value, although it is possible to use a getter and a setter in conjunction to create a type of pseudo-property. Note the following when working with the 0 syntax:
ExamplesDefining a getter on new objects in object initializersThis will create a pseudo-property 5 for object 6, which will return the last array item in 7.
Note that attempting to assign a value to 5 will not change it.Deleting a getter using the { get x() { }, get x() { } } 9 operatorIf you want to remove the getter, you can just 9 it:
Defining a getter on existing objects using { x: /* … */, get x() { /* … */ } } 1To append a getter to an existing object later at any time, use 2.
Using a computed property name
Defining static getters
Smart / self-overwriting / lazy gettersGetters give you a way to define a property of an object, but they do not calculate the property's value until it is accessed. A getter defers the cost of calculating the value until the value is needed. If it is never needed, you never pay the cost. An additional optimization technique to lazify or delay the calculation of a property value and cache it for later access are smart (or memoized) getters. The value is calculated the first time the getter is called, and is then cached so subsequent accesses return the cached value without recalculating it. This is useful in the following situations:
Note: This means that you shouldn't write a lazy getter for a property whose value you expect to change, because if the getter is lazy then it will not recalculate the value. Note that getters are not "lazy" or "memoized" by nature; you must implement this technique if you desire this behavior. In the following example, the object has a getter as its own property. On getting the property, the property is removed from the object and re-added, but implicitly as a data property this time. Finally, the value gets returned.
get vs. definePropertyWhile using the 0 keyword and 2 have similar results, there is a subtle difference between the two when used on 5.When using 0 the property will be defined on the instance's prototype, while using 2 the property will be defined on the instance it is applied to. |