简体   繁体   中英

What does the first 'for' do in 'inheritance' Typescript code?

I was reading the typescript page and in the "Inheritance" section I found this function:

var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};

And when I was reading this line:

for (var p in b)...

I had a question: what does this loop do exactly?

I tried to understand this and I change:

for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];

to

for (var p in b){
    console.log(p);
    if (b.hasOwnProperty(p)) d[p] = b[p];
}

because I wanted to know the value of p , but the console didn't show anything!

At first I thought this for copies b s attributes to d . But I'm not sure right now because I think this loop is not performing any iteration.

I don't understand this for loop. Can anybody help me?
Until now I think that for is useless for that code, but I want to know what you think.

Here is the complete code i have:

var __extends = (this && this.__extends) || function (d, b) {
for (var p in b){
    console.log("iteration");
    console.log(p);
    if (b.hasOwnProperty(p)) d[p] = b[p];
}
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Animal = (function () {
    function Animal(name) {
        this.name = name;
    }
    Animal.prototype.move = function (meters) {
        alert(this.name + " moved " + meters + "m.");
    };
    return Animal;
})();
var Snake = (function (_super) {
    __extends(Snake, _super);
    function Snake(name) {
        _super.call(this, name);
    }
    Snake.prototype.move = function () {
        alert("Slithering...");
        _super.prototype.move.call(this, 5);
    };
    return Snake;
})(Animal);
var Horse = (function (_super) {
    __extends(Horse, _super);
    function Horse(name) {
        _super.call(this, name);
    }
    Horse.prototype.move = function () {
        alert("Galloping...");
        _super.prototype.move.call(this, 45);
    };


return Horse;
})(Animal);
var sam = new Snake("Sammy the Python");
var tom = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);

I don't see anything in console.

A little de-obfuscation will probably make it more obvious.

for (var propertyName in objectToExtend){
    if (objectToExtend.hasOwnProperty(properyName)) {
        targetObject[propertyName] = objectToExtend[propertyName];
}

So it is copying attribute values from the first parameter to the second parameter, but only if those attributes are actually defined on the first parameter itself: this loop will ignore anything defined on the first parameter's prototype.

At first I thought this for copies b s attributes to d .

Indeed it does. d is the class that is going to inherit from b , to be exact, it's the constructor function. This loop does copy all static members (methods and other properties) from one class to the other, so that you can call them on the child constructor as well.

In ES6, this relationship is even made dynamic, where all child constructors inherit prototypically from the parent constructor objects; but usually copying in the definition of the class is enough.

But I'm not sure right now because I think this loop is not performing any iteration.

That might well be, if you don't have any class members declared as static .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM