JS Object的一些注意项

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>


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭