Home Web Front-end JS Tutorial OOP extension for Function_javascript skills

OOP extension for Function_javascript skills

May 16, 2016 pm 06:53 PM
function oop

Copy the code The code is as follows:

// The following is the method used by OOP
// This is very convenient It's obscene...because JS is not an OOP language...
// But the great fans guide us to do this
// Belldandy will bless those who use these methods to OOP...
Function.prototype .inherits = function(base){
//Derivation relationship, prototype is retained
//Only single derivation is supported
this.prototype = new base();
return this;
}
Function.prototype.create = function(){
//The creator of the class is equivalent to using new
//JS does not support the use of call and apply in the constructor, so...
/ /Belldandy, thank you for telling me how to solve this problem...
var _args = [];
for(i=0;i
return eval('new this(' _args.join(',') ')'); //eval is used...Bell, please be nicer next time My idea...
}
Function.prototype.pin = function(pinner,args){
// Register service, or "pin" service
// EventManager can do this
// You can also think of it as implementing an interface with a default implementation...

// For example, pin EventManager can be like this: Class.pin(core.WvwntManager)
args = args || [ ];
pinner.apply(this.prototype,args);
return this;
}
Function.prototype.method = function(name, f) { //Add method, efficient
if (!(f instanceof Function)) throw new Error('Invalid method binding, got type ' typeof f '; expected function');
this.prototype[name] = f;
return this
}
Function.prototype.property = function(name, localName, getter, setter) { //Add properties, you can customize getters and setters
if (!name || !name instanceof String) throw new EnvironmentException('When defining the attribute, the attribute name is not defined or is not a string');
if (!localName || !localName instanceof String) localName = '_local_' name;
if(getter instanceof Function) {
this.prototype['_belldandy_get_' name] = getter;
}
if(setter instanceof Function){
this.prototype['_belldandy_set_' name] = setter;
}
this.prototype[name] = new Function("value , force","
if (!value && !force) {
if (!this['" '_belldandy_get_' name "'] || !this['" '_belldandy_get_' name "'] instanceof Function)
return this['" localName "']; /* when no getter is set*/
else
return this['" '_belldandy_get_ ' name "'].call(this);
} else {
if (!this['" '_belldandy_set_' name "'] || !this['" '_belldandy_set_' name "'] instanceof Function )
this['" localName "'] = value;
else
this['" '_belldandy_set_' name "'].call(this, value);
return this
} ") //Belldandy, forgive me, although this does not produce a closure
return this;
}
Function.prototype.static = function(name,value){ //Static features, including attributes And the method
this[name] = value;
return this;
}

has the following effect:
Copy code The code is as follows:

function foo() { };
foo
.property('a', '_a')
.property('b', '_b', function() { return this._b '.' })
.method('f', function() { dwn(this.a ()) });
function bar(x,y){this.x = x;this.y = y;};
with(bar){
inherits(foo)
method ('g',function(){dwn(this.a() '-' this.b())})
}

var f = new foo();
f.a( 1);
f.b(2);
dwn(f.a());
dwn(f.b());
f.f();
b = bar.create(1,2 );
b.a(4);
b.b(5);
dwn(b.x',' b.y); >
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' 'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' Feb 25, 2024 pm 09:04 PM

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

Application of Golang functions in object-oriented programming Application of Golang functions in object-oriented programming May 31, 2024 pm 07:36 PM

Go functions are available as methods of objects. Methods are functions associated with an object that provide access to the object's fields and methods. In Go, methods are defined using func(receiver_type)identifier(parameters)return_type syntax. This approach plays an important role in object-oriented programming by providing encapsulation, reuse, and extensibility.

The usage and function of Vue.use function The usage and function of Vue.use function Jul 24, 2023 pm 06:09 PM

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

file_exists() function in PHP file_exists() function in PHP Sep 14, 2023 am 08:29 AM

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file

clearstatcache() function in PHP clearstatcache() function in PHP Sep 07, 2023 am 09:33 AM

The clearstatcache() function is used to clear the file status cache. PHP caches the information returned by the following functions −stat()lstat()file_exists()is_writable()is_readable()is_executable()is_file()is_dir()filegroup()fileowner()filesize()filetype()fileperms() What to do To provide better performance. Syntax voidclearstatecache() Parameter NA Return value clearstatcache(

See all articles