Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript OOP帮助/建议/解释_Javascript_Oop_Web - Fatal编程技术网

Javascript OOP帮助/建议/解释

Javascript OOP帮助/建议/解释,javascript,oop,web,Javascript,Oop,Web,嘿,我有个问题。我正在编写一个小的Js对象,以便更轻松地管理我所在的页面,以便能够在每页加载适当的脚本/样式。我遇到了一个我就是不明白的情况。我有一个属性currentPage,显然可以设置为当前页面,但是如果我直接从前面定义的另一个属性设置它,它会返回一个引用错误,但是如果我将它放入一个返回相同内容的函数中,它就会工作。我不知道这是为什么。有人能给我解释一下吗?我不是一个硬核的JS开发者,我只是边走边想,这是JS特有的东西吗?下面是我的意思的代码示例: var self = PageInfo

嘿,我有个问题。我正在编写一个小的Js对象,以便更轻松地管理我所在的页面,以便能够在每页加载适当的脚本/样式。我遇到了一个我就是不明白的情况。我有一个属性
currentPage
,显然可以设置为当前页面,但是如果我直接从前面定义的另一个属性设置它,它会返回一个引用错误,但是如果我将它放入一个返回相同内容的函数中,它就会工作。我不知道这是为什么。有人能给我解释一下吗?我不是一个硬核的JS开发者,我只是边走边想,这是JS特有的东西吗?下面是我的意思的代码示例:

var self = PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(self.locationArray.length);
    },
    //ref. error to locationArray
    parentDirectory : self.locationArray[self.locationArray.length -3],
    currentPage : function() {
        return self.locationArray[self.locationArray.length -2]; // works
    } 
};

当您使用JavaScript对象文字语法(使用大括号创建对象时,
{}
),每个属性的值都是在创建对象时计算的表达式。它们无法引用同一对象的属性,因为该对象尚不存在

请注意,在对象的方法中,可以使用
this
而不是创建
self
变量。只要使用点语法调用方法,如下所示:

PageInfo.currentPage()
…在方法
中,此
将自动引用对象,因此您可以执行以下操作:

var PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(this.locationArray.length);
    },

    currentPage : function() { return this.locationArray[this.locationArray.length -2];}
};

alert( PageInfo.currentPage() );

进一步阅读:

当您使用JavaScript对象文字语法(使用大括号创建对象时,
{}
)每个属性的值都是在创建对象时计算的表达式。它们无法引用同一对象的属性,因为该对象尚不存在

请注意,在对象的方法中,可以使用
this
而不是创建
self
变量。只要使用点语法调用方法,如下所示:

PageInfo.currentPage()
…在方法
中,此
将自动引用对象,因此您可以执行以下操作:

var PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(this.locationArray.length);
    },

    currentPage : function() { return this.locationArray[this.locationArray.length -2];}
};

alert( PageInfo.currentPage() );

进一步阅读:

定义对象时,在创建对象之前不能引用该对象。通过使用函数,您将延迟self.locationArray的查找,直到创建对象为止。

定义对象时,在创建对象之前无法引用该对象。通过使用函数,您将延迟self.locationArray的查找,直到创建对象为止。

对象仅在执行语句后才分配给
self
PageInfo
。 所以在陈述之后再做

var self = PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(self.locationArray.length);
    },

    currentPage : function() { return self.locationArray[self.locationArray.length -2]; // works
    }
};

self.parentDirectory  =  self.locationArray[self.locationArray.length -3];
它还将更新
PageInfo

在函数内部使用
这个
,使其更加面向对象

var self = PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(this.locationArray.length);
    },

    currentPage : function() { return this.locationArray[this.locationArray.length -2]; // works
    }
};

self.parentDirectory  =  self.locationArray[self.locationArray.length -3];       
您还可以创建一个函数来设置
parentDirectory

var self = PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(this.locationArray.length);
    },

    parentDirectory:"",

    setParentDirectory: function() {
         this.parentDirectory  =  this.locationArray[this.locationArray.length -3];  
    },

    currentPage : function() { return this.locationArray[this.locationArray.length -2]; }

};

self.setParentDirectory();

对象仅在执行语句后才分配给
self
PageInfo
。 所以在陈述之后再做

var self = PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(self.locationArray.length);
    },

    currentPage : function() { return self.locationArray[self.locationArray.length -2]; // works
    }
};

self.parentDirectory  =  self.locationArray[self.locationArray.length -3];
它还将更新
PageInfo

在函数内部使用
这个
,使其更加面向对象

var self = PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(this.locationArray.length);
    },

    currentPage : function() { return this.locationArray[this.locationArray.length -2]; // works
    }
};

self.parentDirectory  =  self.locationArray[self.locationArray.length -3];       
您还可以创建一个函数来设置
parentDirectory

var self = PageInfo = {
    locationArray : window.location.pathname.toString().split("/"),

    printOutPath : function(){
        console.log(this.locationArray.length);
    },

    parentDirectory:"",

    setParentDirectory: function() {
         this.parentDirectory  =  this.locationArray[this.locationArray.length -3];  
    },

    currentPage : function() { return this.locationArray[this.locationArray.length -2]; }

};

self.setParentDirectory();

var self=PageInfo={}?也许你想在PageInfo?ouch的上下文中使用它,在第一行定义
self
。另外,您是否正在使用
PageInfoObj.parentDirectory
PageInfoObj.parentDirectory()
(调用或引用)?我添加了self,只是想看看它是否能帮助我。事实并非如此。var PageInfo={}是我如何处理itvar self=PageInfo={}的?也许你想在PageInfo?ouch的上下文中使用它,在第一行定义
self
。另外,您是否正在使用
PageInfoObj.parentDirectory
PageInfoObj.parentDirectory()
(调用或引用)?我添加了self,只是想看看它是否能帮助我。事实并非如此。var PageInfo={}是我如何继续使用它的,谢谢你的提示。我还以为是这样的。如果我按照这样的方式移动:
function PageInfo(){this.locationArray=window.location.pathname.toString().split(“/”)…}
并以这种方式创建对象会更好吗?这取决于您所说的“更好”是什么意思。如果您这样做,那么属性一次创建一个,这样您就可以引用已经创建的属性,但是您需要说
var something=new PageInfo()。如果需要创建多个实例,则构造函数绝对是合适的。另一个选项是在对象文本中创建大多数属性,然后使用
self.parentDirectory=self.locationArray[self.locationArray.length-3]添加
parentDirectory
。谢谢你的提示。我还以为是这样的。如果我按照这样的方式移动:
function PageInfo(){this.locationArray=window.location.pathname.toString().split(“/”)…}
并以这种方式创建对象会更好吗?这取决于您所说的“更好”是什么意思。如果您这样做,那么属性一次创建一个,这样您就可以引用已经创建的属性,但是您需要说
var something=new PageInfo()。如果需要创建多个实例,则构造函数绝对是合适的。另一个选项是在对象文本中创建大多数属性,然后使用
self.parentDirectory=self.locationArray[self.locationArray.length-3]添加
parentDirectory