Skip to content

Private members of outer accessible to class expressions #17535

Closed

Description

@penguin020

(TypeScript Version: 2.4.1 stable)

Code
Here is a use case of class expressions that allows access to the private members of the outer class.

fyi: I actually like this behavior, but it is probably a bug.

    class classX { 
        private y: number = 0; 
    
        public getY(): number { return this.y; }
        
    	public utilities = new class {
    		constructor(public superThis: classX) {
    		}
    		public testSetOuterPrivate(target: number) {
                this.superThis.y = target;
    		}
    	}(this);    
    }
    
    const x1: classX = new classX();

    alert(x1.getY());

    x1.utilities.testSetOuterPrivate(4);
    alert(x1.getY());

codepen

Expected behavior:
this.superThis.y should blow an access error on compilation

Actual behavior:
this.superThis.y does not blow an access error on compilation

Activity

  1. RyanCavanaugh commented on Jul 31, 2017

    @RyanCavanaugh
    Member

    The privacy is determined by the scope of the class body. This code is in the class body, therefore it can access the private member.

  2. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions