Coder Social home page Coder Social logo

Comments (4)

kikito avatar kikito commented on August 22, 2024 1

Yep, @mpeterv is correct. There's another special property in subclasses: super.

When you create a class like so:

local Foo = class('Foo')

The name attribute is automatically set for that class:

assert(Foo.name == 'Foo')

Instances don't have this problem so you can use instance.name however you want (an instance method called name should work, but I am not 100% sure. Just name it something else if it gives you trouble).

I'm closing this down now, but reopen if you need more help.

from middleclass.

qaisjp avatar qaisjp commented on August 22, 2024

from middleclass.

kappe-c avatar kappe-c commented on August 22, 2024

foo is instantiated before you define Foo:name

I'm afraid that doesn't matter:

local class = require 'middleclass'

local Foo = class 'Foo'

function Foo:name() -- equivalent to function Foo.name(self)
	print('method "name" is called')
end

local foo = Foo()
foo:name() -- attempt to call a nil value (method 'name')
print(Foo.name) --> function: 0x123456
print(foo.name) --> nil (btw: that's an ordinary field that could have been set in the constructor)

from middleclass.

mpeterv avatar mpeterv commented on August 22, 2024

name, static, and subclasses properties of a class (plus some private ones starting with underscores) and class property of an instance are special, they don't live in normal namespaces.

from middleclass.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.