关闭

JS Object的一些注意项

20人阅读 评论(0) 收藏 举报

To print out all elements, we can use afor/in loop, like this:

for(var property in dog) {
  console.log(property);
}
//typeof using
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};


// print hello in the 3 different languages
for(var x in languages){
   if(typeof languages[x] ==="string"){
       console.log(languages[x]);
       }else{
        console.log("It's a number!");
    };
    }

*Property can be any placeholder name.
prototype:
className.prototype.newMethod =
function() {
<span style="font-family:Microsoft YaHei;"></pre><br />statements;};</span>
<span style="background-color: rgb(255, 0, 0);"><span style="font-family:Microsoft YaHei;">//Example</span></span>
<span style="font-family:Microsoft YaHei;">function Dog (breed) {
    this.breed = breed;
    Dog.prototype.sayHello = function(){       
    console.log("Hello this is a " +this.breed +" dog");
        }
};
// add the sayHello method to the Dog class 
// so all dogs now can say hello
var yourDog = new Dog("golden retriever");
yourDog.sayHello();</span>
<span style="font-family:Microsoft YaHei;">
var myDog = new Dog("dachshund");
myDog.sayHello();
</span>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21次
    • 积分:10
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档
    阅读排行
    评论排行