开发者

Need help / Creating Methods via prototype property

开发者 https://www.devze.com 2023-03-06 02:36 出处:网络
Please help me to understand below code. This is the script for drag and drop object. I am trying to explore it but struck at one thing.

Please help me to understand below code. This is the script for drag and drop object. I am trying to explore it but struck at one thing.

URL for Reference (Complete Script)

I dont understand how this method creation work, like get x(), set x() etc. Is get 开发者_StackOverflowand Set is predefined object? will it actually set object value or get specific value of object, like we have in date object.

Also there is one space between its declaration set x(), why?

I am new to java script, i really appreciate your help.

// position strings are "x,y" with no units
get x()
{
    return parseInt(this._position.split(',')[0]);
},

set x(inX)
{
    var comps = this._position.split(',');   
    comps[0] = inX;
    this.position = comps.join(',');
},

get y()
{
    return parseInt(this._position.split(',')[1]);
},

set y(inY)
{
    var comps = this._position.split(',');
    comps[1] = inY;
    this.position = comps.join(',');
},


Let's start by calling the language 'JavaScript' not 'java script'; the language is standardized as Ecmascript.

Look up creation of methods in Ecmascript (JavaScript). The functions set(inX) and get() are methods be of some prototype - the clue is the this reference referencing the current instance. However, one should write function get(), and function set(inX) instead.

From Wikipedia:

Prototype-based

prototypes

JavaScript uses prototypes instead of classes for inheritance. It is possible to simulate many class-based features with prototypes in JavaScript. functions as object constructors

Functions double as object constructors along with their typical role. Prefixing a function call with new creates a new object and calls that function with its local this keyword bound to that object for that invocation. The constructor's prototype property determines the object used for the new object's internal prototype. JavaScript's built-in constructors, such as Array, also have prototypes that can be modified. functions as methods

Unlike many object-oriented languages, there is no distinction between a function definition and a method definition. Rather, the distinction occurs during function calling; a function can be called as a method. When a function is called as a method of an object, the function's local this keyword is bound to that object for that invocation.

0

精彩评论

暂无评论...
验证码 换一张
取 消