JavaScript中this的作用域

// var pet = {
//     word: '...',
//     speak: function() {
//         console.log(this.word);
//         console.log(this == pet);
//     }
// }
// pet.speak();
//************************************** */
// function pet(word) {
//     //this can be used in innerfunction
//     this.word = word;
//     console.log(word);
//     console.log(this); //this-->global object
// }
// pet('miao');
//********************************** */
// pet = {
//     words: '...',
//     speak: function(words) {
//         console.log(words);
//     }
// };
// dog = {
//     words: 'wang'
// }
// pet.speak.call(dog, dog.words); //call可以改变上下文,即改变this的指向对象
//***************************************************** */
function pet(words) {
    this.words = words;
    this.speak = function() { console.log(words) };
};


function dog(words) {
    pet.call(this, words);
    //pet.apply(this,words); //have the same use
}
var Dog = new dog('wangwang');
Dog.speak();
//Dog.speak(this.words);

你可能感兴趣的:(nodejs)