hasOwnProperty in JavaScript

前端 未结 4 788
闹比i
闹比i 2020-12-08 05:57

Consider:

function Shape() {
    this.name = "Generic";
    this.draw = function() {
        return "Drawing " + this.name + " Shape&         


        
4条回答
  •  一整个雨季
    2020-12-08 06:46

    Try this:

    function welcomeMessage()
    {
        var shape1 = new Shape();
        //alert(shape1.draw());
        alert(shape1.hasOwnProperty("name"));
    }
    

    When working with reflection in JavaScript, member objects are always refered to as the name as a string. For example:

    for(i in obj) { ... }

    The loop iterator i will be hold a string value with the name of the property. To use that in code you have to address the property using the array operator like this:

     for(i in obj) {
       alert("The value of obj." + i + " = " + obj[i]);
     }
    

提交回复
热议问题