js基礎練習題(4)

9.對象

閱讀代碼,回答問題this

function User(name) {
    var name1 = name;  
    this.name2 = name;  
    function getName1() {  
        return name1;
    }
}
User.prototype.getName2 = function() { 
    return this.name2;
}
User.name3 = 'xiaohong'; 
User.getName3 = function() { 
    return this.name2;
}
var p1 = new User('xiaoqiang');

1.下列代碼輸出結果prototype

console.log(p1.name1)
console.log(p1.getName1())

2.下列代碼輸出結果code

console.log(p1.name3)
console.log(p1.getName3())

3.下列代碼輸出結果對象

console.log(p1.name2)
console.log(p1.getName2)

4.下列代碼輸出結果get

console.log(User.getName3())

5.下列代碼輸出結果io

console.log(User.name1)
console.log(User.getName1())

根據下面代碼,回答問題console

function Foo() {
    getName = function () { alert (1); };
    return this;
}
Foo.getName = function () { alert (2);};
Foo.prototype.getName = function () { alert (3);};
var getName = function () { alert (4);};
function getName() { alert (5);}

1.下面代碼輸出結果function

console.log(Foo.getName())

2.下面代碼輸出結果co

getName();

3.下面代碼輸出結果閱讀

Foo().getName();

4.下列代碼輸出結果

new Foo.getName();
相關文章
相關標籤/搜索