JavaScript access parent object attribute

前端 未结 6 1888
臣服心动
臣服心动 2020-12-31 17:16

I have a small issue in JS, I have two nested objects, and I would like to access a variable from the parent, like so:

var parent = {
    a : 5,

    child:          


        
6条回答
  •  情深已故
    2020-12-31 17:32

    Javascript is prototype based, it is not a regular OOP language like PHP or Java.

    Take a look to Inheritance and the prototype chain and implement something like Simple Javascript inheritance.

    You could probably access to parent through window.parent if it is in the global scope, but your example will not work in every case.

提交回复
热议问题