无法读取JavaScript对象的属性

无法读取JavaScript对象的属性,javascript,object,properties,Javascript,Object,Properties,我的属性读取值工作不正常。它将对象名读取为字符串,而不是对象的属性 我正在尝试创建对象,并根据对象名称设置其属性 然后我只想看到属性,但使用循环。 方法“this.propertyname”运行良好 function bulding(buildingType) { console.log("In main function"); this.room=2; this.clr = "green"; this.scrn = null ; getTy

我的属性读取值工作不正常。它将对象名读取为字符串,而不是对象的属性

我正在尝试创建对象,并根据对象名称设置其属性

然后我只想看到属性,但使用循环。 方法“this.propertyname”运行良好

    function bulding(buildingType)
{  
    console.log("In main function");
    this.room=2;
    this.clr = "green";
    this.scrn = null ;

    getType();

    function getType()
    {  console.log("In control function");
        switch(buildingType){
            case "home":
            setBuildingas(2,"green",null);
            break;

            case "office":
            setBuildingas(20,"white",null);
            break;

            case "mall":
            setBuildingas(200,"asorted",null);
            break;

            case "theater":
            setBuildingas(20,"white","78cm");
            break;

            default:
            console.log("Please Enter Valid Type");
            break;

        }
    }

    function setBuildingas(noOfRooms,buldingColor,theaterScreen){
        this.room=noOfRooms;
        this.clr=buldingColor;
        this.scrn=theaterScreen;
        valueGetter(buildingType);

    }


}

function valueGetter(obj){
  for(var key in obj)
  {     
    console.log(key + " has value of "+obj[key]);       


  }
} 


console.log("In Script");

var house = new bulding("home");

var house2 = new bulding("mall");

var house3 = new bulding("theater");

基本上,function valueGetter并没有让我对属性的所有值进行欲望输出

我修改了您的答案,结果如下:

    function bulding(buildingType)
{  
    console.log("In main function");
// Need to declare "this" in global scope (this in function setBuildingas is already something else!)
    var me = this;
// Set everything to variable "me"
    me.room=2;
    me.clr = "green";
    me.scrn = null ;

    getType();

    function getType()
    {  console.log("In control function");
        switch(buildingType){
            case "home":
            setBuildingas(2,"green",null);
            break;

            case "office":
            setBuildingas(20,"white",null);
            break;

            case "mall":
            setBuildingas(200,"asorted",null);
            break;

            case "theater":
            setBuildingas(20,"white","78cm");
            break;

            default:
            console.log("Please Enter Valid Type");
            break;

        }
    }

    function setBuildingas(noOfRooms,buldingColor,theaterScreen){
        me.room=noOfRooms;
        me.clr=buldingColor;
        me.scrn=theaterScreen;
// Call the valueGetter with the local object "me"
        valueGetter(me);

    }


}

function valueGetter(obj){
  for(var key in obj)
  {     
    console.log(key + " has value of "+obj[key]);       


  }
} 


console.log("In Script");

var house = new bulding("home");

var house2 = new bulding("mall");

var house3 = new bulding("theater");
另外,请参见此处的JSFIDLE:

使用ECMA5的示例

(还有一些其他ECMA5方法只是为了好玩)

理解关键词

Javascript

// Moved here because I like defining things before they are used.
function valueGetter(obj) {
    // Using ECMA5 methods instead, just example
    Object.keys(obj).forEach(function (key) {
        console.log(key + " has value of " + this[key]);
    }, obj);
}

// I like constructors to begin with a capital letter
function Bulding(buildingType) {
    console.log("In main function");
    // Changed to function expression and removed name from function
    var setBuildingas = (function (noOfRooms, buldingColor, theaterScreen) {
        this.room = noOfRooms;
        this.clr = buldingColor;
        this.scrn = theaterScreen;
        // I guess you want to see the contents of 'this' rather than 'buildingType'
        console.log(buildingType);
        valueGetter(this);
    }).bind(this); // added the bind

    this.room = 2;
    this.clr = "green";
    this.scrn = null;
    getType();

    function getType() {
        console.log("In control function");
        switch (buildingType) {
            case "home":
                setBuildingas(2, "green", null);
                break;
            case "office":
                setBuildingas(20, "white", null);
                break;
            case "mall":
                setBuildingas(200, "asorted", null);
                break;
            case "theater":
                setBuildingas(20, "white", "78cm");
                break;
            default:
                console.log("Please Enter Valid Type");
                break;
        }
    }
}

console.log("In Script");
// I like one 'var'
var house = new Bulding("home"),
    house2 = new Bulding("mall"),
    house3 = new Bulding("theater");
// Moved here because I like defining things before they are used.
function valueGetter(obj) {
    // Using ECMA5 methods instead, just example
    Object.keys(obj).forEach(function (key) {
        console.log(key + " has value of " + this[key]);
    }, obj);
}

// I like constructors to begin with a capital letter
function Building(buildingType) {
    console.log("In main function");    
    this.room = 2;
    this.clr = "green";
    this.scrn = null;
    this.buildingType = buildingType;
    this.getType();
}

Building.prototype = {
    setBuildingas: function (noOfRooms, buldingColor, theaterScreen) {
        this.room = noOfRooms;
        this.clr = buldingColor;
        this.scrn = theaterScreen;
        // I guess you want to see the contents of 'this' rather than 'buildingType'
        console.log(this.buildingType);
        valueGetter(this);

        return this;
    },

    getType: function () {
        console.log("In control function");
        switch (this.buildingType) {
            case "home":
                this.setBuildingas(2, "green", null);
                break;
            case "office":
                this.setBuildingas(20, "white", null);
                break;
            case "mall":
                this.setBuildingas(200, "asorted", null);
                break;
            case "theater":
                this.setBuildingas(20, "white", "78cm");
                break;
            default:
                console.log("Please Enter Valid Type");
        }

        return this;
    }
};

console.log("In Script");
// I like one 'var'
var house = new Building("home"),
    house2 = new Building("mall"),
    house3 = new Building("theater");
输出

In Script
In main function
In control function
home
room has value of 2
clr has value of green
scrn has value of null
In main function
In control function
mall
room has value of 200
clr has value of asorted
scrn has value of null
In main function
In control function
theater
room has value of 20
clr has value of white
scrn has value of 78cm 

另一种选择是使用OO并创建一个

Javascript

// Moved here because I like defining things before they are used.
function valueGetter(obj) {
    // Using ECMA5 methods instead, just example
    Object.keys(obj).forEach(function (key) {
        console.log(key + " has value of " + this[key]);
    }, obj);
}

// I like constructors to begin with a capital letter
function Bulding(buildingType) {
    console.log("In main function");
    // Changed to function expression and removed name from function
    var setBuildingas = (function (noOfRooms, buldingColor, theaterScreen) {
        this.room = noOfRooms;
        this.clr = buldingColor;
        this.scrn = theaterScreen;
        // I guess you want to see the contents of 'this' rather than 'buildingType'
        console.log(buildingType);
        valueGetter(this);
    }).bind(this); // added the bind

    this.room = 2;
    this.clr = "green";
    this.scrn = null;
    getType();

    function getType() {
        console.log("In control function");
        switch (buildingType) {
            case "home":
                setBuildingas(2, "green", null);
                break;
            case "office":
                setBuildingas(20, "white", null);
                break;
            case "mall":
                setBuildingas(200, "asorted", null);
                break;
            case "theater":
                setBuildingas(20, "white", "78cm");
                break;
            default:
                console.log("Please Enter Valid Type");
                break;
        }
    }
}

console.log("In Script");
// I like one 'var'
var house = new Bulding("home"),
    house2 = new Bulding("mall"),
    house3 = new Bulding("theater");
// Moved here because I like defining things before they are used.
function valueGetter(obj) {
    // Using ECMA5 methods instead, just example
    Object.keys(obj).forEach(function (key) {
        console.log(key + " has value of " + this[key]);
    }, obj);
}

// I like constructors to begin with a capital letter
function Building(buildingType) {
    console.log("In main function");    
    this.room = 2;
    this.clr = "green";
    this.scrn = null;
    this.buildingType = buildingType;
    this.getType();
}

Building.prototype = {
    setBuildingas: function (noOfRooms, buldingColor, theaterScreen) {
        this.room = noOfRooms;
        this.clr = buldingColor;
        this.scrn = theaterScreen;
        // I guess you want to see the contents of 'this' rather than 'buildingType'
        console.log(this.buildingType);
        valueGetter(this);

        return this;
    },

    getType: function () {
        console.log("In control function");
        switch (this.buildingType) {
            case "home":
                this.setBuildingas(2, "green", null);
                break;
            case "office":
                this.setBuildingas(20, "white", null);
                break;
            case "mall":
                this.setBuildingas(200, "asorted", null);
                break;
            case "theater":
                this.setBuildingas(20, "white", "78cm");
                break;
            default:
                console.log("Please Enter Valid Type");
        }

        return this;
    }
};

console.log("In Script");
// I like one 'var'
var house = new Building("home"),
    house2 = new Building("mall"),
    house3 = new Building("theater");

不在ECMA5环境中,则有垫片可用,也可以使用库。在许多现代JavaScript库中都存在.bind或与之等价的函数

例如

  • 洛达斯/下划线
  • 您还可以使用一个解决方案来避免该问题,或者甚至是

    沃尔德隆

    最后,使用
    self
    作为别名,为
    创建别名 标识符。这是非常容易出现错误的,无论何时都应该避免 可能


    这是无数其他问题的翻版。添加
    var obj=this
    到“building”函数的顶部,然后将其中的所有引用和嵌套函数从
    this
    更改为
    obj
    。如果使用ECMA5,您也可以使用可能的副本,您应该解释更改的内容。谢谢。它起作用了。然而,为什么在函数getType中我们只需要使用原始变量来代替新变量“me”??我甚至试着将“我”传递给getType,但没有成功