AngularJs, why is updating a property better than updating varibale?

from "ng-book", it says:

Due to the nature of JavaScript itself and how it passes the meaning compared to the link, it is considered the best practice in Angular to bind links in views by the attribute of the object, and not to the raw object itself.

.....

In this case, instead of updating $ scope.clock every second, we can update the clock.now property. With this optimization we can ...

I don’t know why, because the "JavaScript: The Definitive Guide" says:

Is there any fundamental difference between the variable i and the property i of the object o? The answer is no. The variables in JavaScript are basically the same as the properties of an object.

In this book:

$scope.time = { now: new Date()} 

better than

$socpe.time = new Date();
+4
2

HTML:

<div ng-app>
    <div ng-controller="MyController">
        <div>{{rawObj}}</div>
        <div>{{obj.prop}}</div>
        <div ng-if="isShown">
            <input ng-model="rawObj" />
            <input ng-model="obj.prop" />
            <span>{{readOnly}}</span>
        </div>
    </div>
</div>

js :

 function MyController($scope){
    $scope.rawObj = "raw value";
    $scope.obj = {
        prop: "property of object"
    }
    $scope.readOnly = "read only";
    $scope.isShown = true;
 }

ng-model = "rawObj", rawObj as $scope MyController , rawObj ng-if. ng-if div, . , ng- ( ), . , : ng-if ng-repeat.

ng-, "" .

: http://jsfiddle.net/VC5WK/

, javascript answears.

+2

$scope .

//In controller A:
$scope.time = 1;

//In controller B:
alert($scope.time) may not be 1.

, $scope , $scope. - , $scope, .

javascript:

var objectA = {time:1, prop:{time:1}};

//setup inheritance
function classB() {}
classB.prototype = objectA;

//objectB inherits the same properties as objectA
var objectB = new classB();

objectB.time = 2;
objectB.prop.time = 2;

alert(objectA.time); //=1 changes to B were not transfered to A
alert(objectA.prop.time); //=2
0

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


All Articles