Comments (4)
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.
from middleclass.
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.
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)
- Wiki doesn't say that you can create instances with __call HOT 5
- class.name doesn't work HOT 4
- Documentation? HOT 1
- __newindex declaration makes it impossible to have public attributes HOT 5
- What is the proper means of creating a module out of a middleclass class hierarchy HOT 3
- Sample not working HOT 1
- How to split a big class to several files? HOT 2
- There's a bug in line 82
- Make isSubclassOf and isInstanceOf accept strings HOT 4
- Implementing destructors HOT 1
- Maybe somekind offtop, but how to generally determine if variable is any class (and not primitive etc) before checking if it's instance of class? HOT 7
- properties, sorry to ask lol HOT 1
- Make it possible to include superclasses and mixins by string names HOT 2
- Don't understand how to work with metamethods :( HOT 5
- Question regarding method inheritance and overide when there are parameters HOT 4
- aClass.subclasses never used? Why this field is always empty? HOT 2
- `includes` method to check if a class includes a mixin HOT 1
- Static inheritance seems broken
- Hot reloading of instances and local up values
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from middleclass.